Permalink
Browse files

Documentation update

  • Loading branch information...
MattTuttle committed Feb 27, 2014
1 parent 2dbf87e commit 1f0cd19a6fb32dad92d5d89b614bd545f6a29b07
Showing with 117 additions and 9 deletions.
  1. +4 −0 doc/.gitignore
  2. +18 −0 doc/doc.hxml
  3. +30 −5 hxnet/protocols/RPC.hx
  4. +25 −1 hxnet/protocols/Telnet.hx
  5. +40 −3 hxnet/protocols/WebSocket.hx
View
@@ -0,0 +1,4 @@
cpp.xml
neko.xml
flash.xml
*.docset/
View
@@ -0,0 +1,18 @@
-cp ..
--no-output
--macro include('hxnet')
--each
-neko hxnet.n
-xml neko.xml
--next
-cpp hxnet.cpp
-xml cpp.xml
--next
-swf hxnet.swf
-xml flash.xml
View
@@ -10,11 +10,20 @@ import neko.Lib;
import cpp.Lib;
#end
/**
* Remote Procedure Call protocol
*/
class RPC extends hxnet.base.Protocol
{
public var dispatcher:Dynamic;
/**
* The object to call methods on. Defaults to the RPC protocol object.
*/
public var dispatcher:Dynamic = this;
/**
* When a full packet is received the method and arguments are read and executed.
*/
override private function packetReceived(input:Input)
{
var func = readString(input);
@@ -46,10 +55,12 @@ class RPC extends hxnet.base.Protocol
dispatch(func, arguments);
}
/**
* Dispatches a method with arguments
* @throws Invalid calls when dispatcher object does not contain the method or the declaration doesn't match the number of arguments passed.
*/
private inline function dispatch(func:String, arguments:Array<Dynamic>)
{
if (dispatcher == null) dispatcher = this;
try
{
var rpcCall = Reflect.field(dispatcher, func);
@@ -69,12 +80,17 @@ class RPC extends hxnet.base.Protocol
}
}
public function call(func:String, ?arguments:Array<Dynamic>)
/**
* Calls a procedure remotely through connection
* @param method The method to call on the remote end
* @param arguments The method arguments to use on the remote end
*/
public function call(method:String, ?arguments:Array<Dynamic>)
{
if (arguments == null) arguments = [];
var o = new BytesOutput();
writeString(o, func);
writeString(o, method);
o.writeInt16(arguments.length);
for (arg in arguments)
{
@@ -114,12 +130,21 @@ class RPC extends hxnet.base.Protocol
cnx.writeBytes(o.getBytes(), true);
}
/**
* Convenience method to read string from input
* @param i Input object to read from
*/
private inline function readString(i:Input):String
{
var len = i.readInt32();
return i.readString(len);
}
/**
* Convenience method to write string to output
* @param o Output object to write to
* @param value String value to write
*/
private inline function writeString(o:BytesOutput, value:String)
{
o.writeInt32(value.length);
View
@@ -4,8 +4,16 @@ import haxe.io.Input;
import haxe.io.Bytes;
import haxe.io.BytesOutput;
/**
* Telnet protocol
*/
class Telnet extends hxnet.base.Protocol
{
/**
* Upon receiving data this method is called
* @param input The input to read from
*/
override public function dataReceived(input:Input)
{
var buffer = input.readLine();
@@ -33,12 +41,20 @@ class Telnet extends hxnet.base.Protocol
lineReceived(buffer);
}
/**
* Send a line of text over the connection
* @param data The string data to write
*/
public function writeLine(data:String)
{
data += "\r\n";
cnx.writeBytes(Bytes.ofString(data));
}
/**
* Turns echo on/off on the remote side. Useful for password entry.
* @param show Whether to show keyboard output on the remote connection.
*/
public function echo(show:Bool = true)
{
var out = new BytesOutput();
@@ -50,6 +66,11 @@ class Telnet extends hxnet.base.Protocol
cnx.writeBytes(out.getBytes());
}
/**
* Prompt the user for feedback and return answer to callback
* @param prompt The line of text to prompt user for input
* @param callback The method to return the user's response
*/
public function prompt(prompt:String, callback:String->Bool)
{
promptBytes = Bytes.ofString(prompt + " ");
@@ -60,5 +81,8 @@ class Telnet extends hxnet.base.Protocol
private var promptBytes:Bytes;
private var promptCallback:String->Bool;
/**
* Overridable method when a line is received. Used in subclasses.
*/
private function lineReceived(line:String) { }
}
}
@@ -13,6 +13,9 @@ import neko.Lib;
import cpp.Lib;
#end
/**
* WebSocket protocol (RFC 6455)
*/
class WebSocket extends hxnet.base.Protocol
{
private static inline var WEBSOCKET_VERSION = 13;
@@ -32,6 +35,9 @@ class WebSocket extends hxnet.base.Protocol
private var key:String;
private var origin:String;
/**
* Construct the WebSocket protocol
*/
public function new(url:String, host:String, port:Int, origin:String, key:String="key")
{
super();
@@ -43,6 +49,10 @@ class WebSocket extends hxnet.base.Protocol
this.origin = origin;
}
/**
* Upon connecting with another WebSocket send handshake
* @param cnx The remote connection
*/
override public function makeConnection(cnx:Connection)
{
super.makeConnection(cnx);
@@ -64,6 +74,10 @@ class WebSocket extends hxnet.base.Protocol
headersSent = true;
}
/**
* When data is received for the protocol this method is called.
* @param input The input data
*/
override public function dataReceived(input:Input)
{
if (headersSent)
@@ -99,20 +113,37 @@ class WebSocket extends hxnet.base.Protocol
}
}
/**
* Overridable functions for receiving text
*/
private function recvText(text:String) { }
/**
* Overridable functions for receiving binary data
*/
private function recvBinary(data:Bytes) { }
/**
* Sends text over connection
*/
public function sendText(text:String)
{
sendFrame(0x1, Bytes.ofString(text));
sendFrame(OPCODE_TEXT, Bytes.ofString(text));
}
/**
* Sends binary data over connection
*/
public function sendBinary(bytes:Bytes)
{
sendFrame(0x2, bytes);
sendFrame(OPCODE_BINARY, bytes);
}
/**
* Sends a frame of data (text, binary, ping)
* @param opcode Value of the WebSocket protocol opcode
* @param bytes The data to send, if any
*/
private function sendFrame(opcode:Int, ?bytes:haxe.io.Bytes)
{
var bytes = new BytesOutput();
@@ -128,6 +159,9 @@ class WebSocket extends hxnet.base.Protocol
cnx.writeBytes(bytes.getBytes());
}
/**
* Reads a complete WebSocket frame
*/
private inline function recvFrame(input:Input)
{
var opcode = input.readByte();
@@ -174,7 +208,10 @@ class WebSocket extends hxnet.base.Protocol
};
}
private function encodeBase64(content:String) : String
/**
* Helper function to encode content into base 64
*/
private function encodeBase64(content:String):String
{
var suffix = switch (content.length % 3) {
case 2: "=";

0 comments on commit 1f0cd19

Please sign in to comment.