Skip to content

Commit

Permalink
Added WebSocket.receiveBinary() and receiveText() to replace the non-…
Browse files Browse the repository at this point in the history
…explicit receive(). Fixes #182.

Also changed the example to use readText().
  • Loading branch information
s-ludwig committed Mar 15, 2013
1 parent 04381e3 commit 7911d20
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 68 deletions.
5 changes: 3 additions & 2 deletions source/vibe/core/drivers/threadedfile.d
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class ThreadedFileStream : FileStream {
@property int fd() { return m_fileDescriptor; }
@property Path path() const { return m_path; }
@property ulong size() const { return m_size; }
@property bool readable() const { return m_mode == FileMode.Read; }
@property bool readable() const { return m_mode != FileMode.Append; }
@property bool writable() const { return m_mode != FileMode.Read; }

void acquire()
Expand Down Expand Up @@ -193,7 +193,8 @@ class ThreadedFileStream : FileStream {
{
assert(this.writable);
assert(bytes.length <= int.max);
enforce(.write(m_fileDescriptor, bytes.ptr, cast(int)bytes.length) == bytes.length, "Failed to write data to disk.");
auto ret = .write(m_fileDescriptor, bytes.ptr, cast(int)bytes.length);
enforce(ret == bytes.length, "Failed to write data to disk."~to!string(bytes.length)~" "~to!string(errno)~" "~to!string(ret)~" "~to!string(m_fileDescriptor));
m_ptr += bytes.length;
}

Expand Down
127 changes: 64 additions & 63 deletions source/vibe/db/mongo/database.d
Original file line number Diff line number Diff line change
Expand Up @@ -11,79 +11,80 @@ import vibe.data.bson;

struct MongoDatabase
{
private:
private {
string m_name;
MongoClient m_client;
}

package:
// http://www.mongodb.org/display/DOCS/Commands
Bson runCommand(Bson commandAndOptions)
{
return m_client.getCollection(m_name ~ ".$cmd").findOne(commandAndOptions);
}
// http://www.mongodb.org/display/DOCS/Commands
package Bson runCommand(Bson commandAndOptions)
{
return m_client.getCollection(m_name ~ ".$cmd").findOne(commandAndOptions);
}

public:
this(MongoClient client, string name)
{
import std.algorithm;
//@disable this();

assert(client !is null);
m_client = client;
this(MongoClient client, string name)
{
import std.algorithm;

assert(
!canFind(name, '.'),
"Compound collection path provided to MongoDatabase constructor instead of single database name"
);
m_name = name;
}
assert(client !is null);
m_client = client;

@property string name()
{
return m_name;
}
assert(
!canFind(name, '.'),
"Compound collection path provided to MongoDatabase constructor instead of single database name"
);
m_name = name;
}

@property MongoClient client()
{
return m_client;
}
@property string name()
{
return m_name;
}

/**
* Returns: child collection of this database named "name"
*/
MongoCollection opIndex(string name)
{
return MongoCollection(this, name);
}
@property MongoClient client()
{
return m_client;
}

/**
* Returns: struct storing data from MongoDB db.getLastErrorObj() object
*
* Exact object format is not documented. MongoErrorDescription signature will be
* updated upon any issues. Note that this method will execute a query to service
* collection and thus is far from being "free".
*/
MongoErrorDescription getLastError()
{
return m_client.lockConnection().getLastError(m_name);
}
/**
* Returns: child collection of this database named "name"
*/
MongoCollection opIndex(string name)
{
return MongoCollection(this, name);
}

/* See $(LINK http://www.mongodb.org/display/DOCS/getLog+Command)
*
* Returns: Bson document with recent log messages from MongoDB service.
* Params:
* mask = "global" or "rs" or "startupWarnings". Refer to official MongoDB docs.
*/
Bson getLog(string mask)
{
return runCommand(Bson(["getLog" : Bson(mask)]));
}
/**
* Returns: struct storing data from MongoDB db.getLastErrorObj() object
*
* Exact object format is not documented. MongoErrorDescription signature will be
* updated upon any issues. Note that this method will execute a query to service
* collection and thus is far from being "free".
*/
MongoErrorDescription getLastError()
{
return m_client.lockConnection().getLastError(m_name);
}

/* See $(LINK http://www.mongodb.org/display/DOCS/fsync+Command)
*
* Returns: check documentation
*/
Bson fsync(bool async = false)
{
return runCommand(Bson(["fsync" : Bson(1), "async" : Bson(async)]));
}
/* See $(LINK http://www.mongodb.org/display/DOCS/getLog+Command)
*
* Returns: Bson document with recent log messages from MongoDB service.
* Params:
* mask = "global" or "rs" or "startupWarnings". Refer to official MongoDB docs.
*/
Bson getLog(string mask)
{
return runCommand(Bson(["getLog" : Bson(mask)]));
}

/* See $(LINK http://www.mongodb.org/display/DOCS/fsync+Command)
*
* Returns: check documentation
*/
Bson fsync(bool async = false)
{
return runCommand(Bson(["fsync" : Bson(1), "async" : Bson(async)]));
}
}
27 changes: 24 additions & 3 deletions source/vibe/http/websockets.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{
// simple echo server
while( sock.connected ){
auto msg = sock.receive();
auto msg = sock.receiveText();
sock.send(msg);
}
}
Expand Down Expand Up @@ -171,13 +171,34 @@ class WebSocket {

/**
Receives a new message and returns its contents as a newly allocated data array.
Params:
strict = If set, ensures the exact frame type (text/binary) is received and throws an execption otherwise.
*/
ubyte[] receive()
ubyte[] receiveBinary(bool strict = false)
{
ubyte[] ret;
receive((scope message){ ret = message.readAll(); });
receive((scope message){
enforce(!strict || message.frameOpcode == FrameOpcode.binary,
"Expected a binary message, got "~message.frameOpcode.to!string());
ret = message.readAll();
});
return ret;
}
/// ditto
string receiveText(bool strict = false)
{
string ret;
receive((scope message){
enforce(!strict || message.frameOpcode == FrameOpcode.text,
"Expected a text message, got "~message.frameOpcode.to!string());
ret = message.readAllUtf8();
});
return ret;
}

/// Compatibility alias for readBinary. Will be deprecated at some point.
alias receive = receiveBinary;

/**
Receives a new message using an InputStream.
Expand Down

0 comments on commit 7911d20

Please sign in to comment.