public void ProcessMessage(byte[] bytes) { var stream = new SpStream(bytes, 0, bytes.Length, bytes.Length); var result = _rpc.Dispatch(stream); _client.ProcessMessage(result); }
private void Receive(IAsyncResult asyncResult) { int receiveLength = mSocket.EndReceive(asyncResult); int read = 0; while (receiveLength > read) { if (read >= mReceiveBuffer.Length - 1) { break; } int size = (mReceiveBuffer[read] | mReceiveBuffer[read + 1] << 8); read += 2; if (receiveLength >= read + size) { SpStream spStream = new SpStream(mReceiveBuffer, read, size, size); SpRpcResult spResult = mRpc.Dispatch(spStream); mReceiveQueue.Enqueue(spResult); } read += size; } mSocket.BeginReceive(mReceiveBuffer, 0, mReceiveBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), this); }
public bool Request(string proto, SpObject args, int session, SpStream stream) { SpStream encode_stream = EncodeRequest(proto, args, session); encode_stream.Position = 0; return(SpPacker.Pack(encode_stream, stream)); }
public SpStream Encode(SpType type, SpObject obj) { SpStream stream = new SpStream(); if (Encode(type, obj, stream) == false) { if (stream.IsOverflow()) { if (stream.Position > MAX_SIZE) { return(null); } int size = stream.Position; size = ((size + 7) / 8) * 8; stream = new SpStream(size); if (Encode(type, obj, stream) == false) { return(null); } } else { return(null); } } return(stream); }
private static int PackSeg (byte[] src, int src_offset, SpStream output, int n) { byte header = 0; int notzero = 0; int dest_begin = output.Position; output.Position++; for (int i = 0; i < 8; i++) { if (src[src_offset + i] != 0) { notzero++; header |= (byte)(1 << i); output.Write (src[src_offset + i]); } } if ((notzero == 7 || notzero == 6) && n > 0) notzero = 8; if (notzero == 8) { if (n > 0) return 8; else return 10; } int dest_end = output.Position; output.Position = dest_begin; output.Write (header); output.Position = dest_end; return notzero + 1; }
private void TestFoo() { SpStream req = client.Request("foo", null, 2); Util.Assert(req.Length == 4); Util.Log("request foo size = " + req.Length); req.Position = 0; SpRpcResult dispatch_result = server.Dispatch(req); SpObject foobar_response = new SpObject(); foobar_response.Insert("ok", false); SpStream resp = server.Response(dispatch_result.Session, foobar_response); Util.Assert(resp.Length == 7); Util.Log("response package size = " + resp.Length); Util.Log("client dispatch"); resp.Position = 0; dispatch_result = client.Dispatch(resp); Util.Assert(dispatch_result.Arg["ok"].AsBoolean() == false); Util.DumpObject(dispatch_result.Arg); }
private void TestBlackhole() { SpStream req = client.Request("blackhole"); Util.Assert(req.Length == 3); Util.Log("request blackhole size = " + req.Length); }
private void TestFoobar() { Util.Log("client request foobar"); SpObject foobar_request = new SpObject(); foobar_request.Insert("what", "foo"); SpStream req = client.Request("foobar", foobar_request, 1); Util.Assert(req.Length == 11); Util.Log("request foobar size = " + req.Length); req.Position = 0; SpRpcResult dispatch_result = server.Dispatch(req); Util.Assert(dispatch_result.Arg["what"].AsString().Equals("foo")); Util.DumpObject(dispatch_result.Arg); Util.Log("server response"); SpObject foobar_response = new SpObject(); foobar_response.Insert("ok", true); SpStream resp = server.Response(dispatch_result.Session, foobar_response); Util.Assert(resp.Length == 7); Util.Log("response package size = " + resp.Length); Util.Log("client dispatch"); resp.Position = 0; dispatch_result = client.Dispatch(resp); Util.Assert(dispatch_result.Arg["ok"].AsBoolean() == true); Util.DumpObject(dispatch_result.Arg); }
public void Run () { SpTypeManager manager = LoadProto (); // different ways to create SpObject Util.Log ("Object Create Check"); CheckObj (CreateObject ()); CheckObj (CreateObject2 ()); SpObject obj = CreateObject3 (); CheckObj (obj); Util.Log ("Encode"); SpStream small_stream = new SpStream (32); bool success = manager.Codec.Encode ("foobar", obj, small_stream); Util.Assert (success == false); Util.Log ("encode failed! require size : " + small_stream.Position); small_stream.Position = 0; Util.DumpStream (small_stream); SpStream encode_stream = manager.Codec.Encode ("foobar", obj); encode_stream.Position = 0; Util.DumpStream (encode_stream); Util.Log ("Decode "); encode_stream.Position = 0; SpObject ooo = manager.Codec.Decode ("foobar", encode_stream); CheckObj (ooo); Util.Log ("Pack"); encode_stream.Position = 0; small_stream.Position = 0; success = SpPacker.Pack (encode_stream, small_stream); Util.Assert (success == false); Util.Log ("pack failed! require size : " + small_stream.Position); small_stream.Position = 0; Util.DumpStream (small_stream); SpStream pack_stream = SpPacker.Pack (encode_stream); pack_stream.Position = 0; Util.DumpStream (pack_stream); Util.Log ("Unpack"); pack_stream.Position = 0; small_stream.Position = 0; success = SpPacker.Unpack (pack_stream, small_stream); Util.Assert (success == false); Util.Log ("unpack failed! require size : " + small_stream.Position); small_stream.Position = 0; Util.DumpStream (small_stream); pack_stream.Position = 0; SpStream decode_stream = SpPacker.Unpack (pack_stream); decode_stream.Position = 0; Util.DumpStream (decode_stream); Util.Log ("Decode "); decode_stream.Position = 0; SpObject newObj = manager.Codec.Decode ("foobar", decode_stream); CheckObj (newObj); }
protected void Button1_Click() { //THIS BLOCK WORKS RIGHT SpVoice ob = new SpVoice(); ob.Rate = -100; ob.Speak("Start recognistion", SpeechVoiceSpeakFlags.SVSFDefault); SpStream str = new SpStream(); }
public bool Encode (SpType type, SpObject obj, SpStream stream) { if (type == null || obj == null || stream == null) return false; mStream = stream; bool success = EncodeInternal (type, obj); return (success && stream.IsOverflow () == false); }
public static void WriteFF (byte[] src, int src_offset, SpStream output, int n, int correctPos) { int align8_n = (n + 7) & (~7); output.Write ((byte)0xff); output.Write ((byte)(align8_n / 8 - 1)); output.Write (src, src_offset, n); for (int i = 0; i < align8_n - n; i++) output.Write ((byte)0); output.CorrectLength (correctPos); }
private void TestBar() { SpStream req = client.Request("bar"); Util.Assert(req.Length == 3); Util.Log("request bar size = " + req.Length); req.Position = 0; server.Dispatch(req); }
public SpObject Decode(SpType type, SpStream stream) { if (type == null || stream == null) { return(null); } mStream = stream; return(DecodeInternal(type)); }
public static bool Unpack(SpStream input, SpStream output) { int src_size = input.Tail; int src_offset = input.Offset; byte[] src = input.Buffer; while (src_offset < src_size) { byte header = src[src_offset]; src_offset++; if (header == 0xff) { if (src_offset > src_size) { return(false); } int n = (src[src_offset] + 1) * 8; if (src_size - src_offset < n + 1) { return(false); } src_offset++; output.Write(src, src_offset, n); src_offset += n; } else { for (int i = 0; i < 8; i++) { int nz = (header >> i) & 1; if (nz != 0) { if (src_offset > src_size) { return(false); } output.Write(src[src_offset]); src_offset++; } else { output.Write((byte)0); } } } } return(output.IsOverflow() == false); }
public static void DumpStream (SpStream stream) { string str = ""; byte[] buf = new byte[16]; int count; while ((count = stream.Read (buf, 0, buf.Length)) > 0) { str += DumpLine (buf, count); } Log (str); }
public bool Request (string proto, SpObject args, int session, SpStream stream) { Util.Log ("requst input==================================="); Util.Log ("proto======================="+proto); Util.DumpObject (args); Util.DumpStream (stream); SpStream encode_stream = EncodeRequest (proto, args, session); Util.Log ("dump encode stream==============================="); Util.DumpStream (encode_stream); encode_stream.Position = 0; return SpPacker.Pack (encode_stream, stream); }
public bool Encode(SpType type, SpObject obj, SpStream stream) { if (type == null || obj == null || stream == null) { return(false); } mStream = stream; bool success = EncodeInternal(type, obj); return(success && stream.IsOverflow() == false); }
public static void WriteFF(byte[] src, int src_offset, SpStream output, int n, int correctPos) { int align8_n = (n + 7) & (~7); output.Write((byte)0xff); output.Write((byte)(align8_n / 8 - 1)); output.Write(src, src_offset, n); for (int i = 0; i < align8_n - n; i++) { output.Write((byte)0); } output.CorrectLength(correctPos); }
public void SendRequest(string protocolName, SpObject spObject) { mSession++; SpStream spStream = new SpStream(); spStream.Write((short)0); mRpc.Request(protocolName, spObject, mSession, spStream); int len = spStream.Length - 2; spStream.Buffer[0] = (byte)((len >> 8) & 0xff); spStream.Buffer[1] = (byte)(len & 0xff); mSocket.BeginSend(spStream.Buffer, 0, spStream.Length, SocketFlags.None, new AsyncCallback(SendRequestCallback), this); }
public static void DumpStream(SpStream stream) { string str = ""; byte[] buf = new byte[16]; int count; while ((count = stream.Read(buf, 0, buf.Length)) > 0) { str += DumpLine(buf, count); } Log(str); }
public SpRpcResult Dispatch(SpStream stream) { SpStream unpack_stream = SpPacker.Unpack(stream); unpack_stream.Position = 0; SpObject header = mHostTypeManager.Codec.Decode(mHeaderType, unpack_stream); int session = 0; if (header["session"] != null) { session = header["session"].AsInt(); } if (header["type"] != null) { // handle request SpProtocol protocol = mHostTypeManager.GetProtocolByTag(header["type"].AsInt()); SpObject obj = mHostTypeManager.Codec.Decode(protocol.Request, unpack_stream); if (session != 0) { mSessions[session] = protocol; } return(new SpRpcResult(session, protocol, SpRpcOp.Request, obj)); } else { // handle response if (mSessions.ContainsKey(session) == false) { return(new SpRpcResult()); } SpProtocol protocol = mSessions[session]; mSessions.Remove(session); if (protocol == null) { return(new SpRpcResult()); } if (protocol.Response == null) { return(new SpRpcResult(session, protocol, SpRpcOp.Response, null)); } SpObject obj = mAttachTypeManager.Codec.Decode(protocol.Response, unpack_stream); return(new SpRpcResult(session, protocol, SpRpcOp.Response, obj)); } }
public static SpStream Pack (SpStream input) { SpStream stream = new SpStream (); int pos = input.Position; if (Pack (input, stream) == false) { int size = stream.Position; size = ((size + 7) / 8) * 8; stream = new SpStream (size); input.Position = pos; // do we need this check? it's not possible it will return false this time if (Pack (input, stream) == false) return null; } return stream; }
public void Recv(IAsyncResult ar) { int ret = mSocket.EndReceive(ar); if (ret > 0) { mRecvOffset += ret; int read = 0; while (mRecvOffset > read) { int size = (mRecvBuffer[read + 1] | (mRecvBuffer[read + 0] << 8)); read += 2; if (mRecvOffset >= size + read) { SpStream stream = new SpStream(mRecvBuffer, read, size, size); SpRpcResult result = mRpc.Dispatch(stream); switch (result.Op) { case SpRpcOp.Request: Util.Log("Recv Request : " + result.Protocol.Name + ", session : " + result.Session); if (result.Arg != null) { Util.DumpObject(result.Arg); } break; case SpRpcOp.Response: Util.Log("Recv Response : " + result.Protocol.Name + ", session : " + result.Session); if (result.Arg != null) { Util.DumpObject(result.Arg); } break; } read += size; } } Util.Assert(mRecvOffset == read); mRecvOffset = 0; } mSocket.BeginReceive(mRecvBuffer, 0, mRecvBuffer.Length, SocketFlags.None, new System.AsyncCallback(ReadCallback), this); }
public void Run() { SpTypeManager manager = LoadProto(); SpObject obj = CreateObject(); CheckObj(obj); Util.Log("Encode"); SpStream encode_stream = new SpStream(); manager.Codec.Encode("AddressBook", obj, encode_stream); encode_stream.Position = 0; Util.DumpStream(encode_stream); Util.Log("Decode"); encode_stream.Position = 0; SpObject newObj = manager.Codec.Decode("AddressBook", encode_stream); CheckObj(newObj); Util.Log("Pack"); encode_stream.Position = 0; SpStream pack_stream = new SpStream(); SpPacker.Pack(encode_stream, pack_stream); pack_stream.Position = 0; Util.DumpStream(pack_stream); Util.Log("Unpack"); pack_stream.Position = 0; SpStream unpack_stream = new SpStream(); SpPacker.Unpack(pack_stream, unpack_stream); unpack_stream.Position = 0; Util.DumpStream(unpack_stream); Util.Log("Decode"); unpack_stream.Position = 0; newObj = manager.Codec.Decode("AddressBook", unpack_stream); CheckObj(newObj); }
private static int PackSeg(byte[] src, int src_offset, SpStream output, int n) { byte header = 0; int notzero = 0; int dest_begin = output.Position; output.Position++; for (int i = 0; i < 8; i++) { if (src[src_offset + i] != 0) { notzero++; header |= (byte)(1 << i); output.Write(src[src_offset + i]); } } if ((notzero == 7 || notzero == 6) && n > 0) { notzero = 8; } if (notzero == 8) { if (n > 0) { return(8); } else { return(10); } } int dest_end = output.Position; output.Position = dest_begin; output.Write(header); output.Position = dest_end; return(notzero + 1); }
public static SpStream Unpack(SpStream input) { SpStream stream = new SpStream(); int pos = input.Position; if (Unpack(input, stream) == false) { stream = new SpStream(stream.Position); input.Position = pos; if (Unpack(input, stream) == false) { return(null); } } return(stream); }
private void TestStr(string s) { SpObject obj = new SpObject(SpObject.ArgType.Table, "a", s); Util.Log("------------------TestStr----------------------------"); Util.Log(s); Util.Log("Encode"); SpStream encode_stream = new SpStream(2); bool ret = manager.Codec.Encode("ss", obj, encode_stream); Util.Assert(ret == false); encode_stream = new SpStream(encode_stream.Position); ret = manager.Codec.Encode("ss", obj, encode_stream); Util.Assert(ret == true); encode_stream.Position = 0; Util.DumpStream(encode_stream); Util.Log("Pack"); encode_stream.Position = 0; SpStream pack_stream = new SpStream(); SpPacker.Pack(encode_stream, pack_stream); pack_stream.Position = 0; Util.DumpStream(pack_stream); Util.Log("Unpack"); pack_stream.Position = 0; SpStream unpack_stream = new SpStream(); SpPacker.Unpack(pack_stream, unpack_stream); unpack_stream.Position = 0; Util.DumpStream(unpack_stream); Util.Log("Decode"); unpack_stream.Position = 0; SpObject dobj = manager.Codec.Decode("ss", unpack_stream); string ds = dobj["a"].AsString(); Util.Log(ds); Util.Assert(s == ds); }
public SpStream Encode (SpType type, SpObject obj) { SpStream stream = new SpStream (); if (Encode (type, obj, stream) == false) { if (stream.IsOverflow ()) { if (stream.Position > MAX_SIZE) return null; int size = stream.Position; size = ((size + 7) / 8) * 8; stream = new SpStream (size); if (Encode (type, obj, stream) == false) return null; } else { return null; } } return stream; }
public void Run () { SpTypeManager manager = LoadProto (); SpObject obj = CreateObject (); CheckObj (obj); Util.Log ("Encode"); SpStream encode_stream = new SpStream (); manager.Codec.Encode ("AddressBook", obj, encode_stream); encode_stream.Position = 0; Util.DumpStream (encode_stream); Util.Log ("Decode"); encode_stream.Position = 0; SpObject newObj = manager.Codec.Decode ("AddressBook", encode_stream); CheckObj (newObj); Util.Log ("Pack"); encode_stream.Position = 0; SpStream pack_stream = new SpStream (); SpPacker.Pack (encode_stream, pack_stream); pack_stream.Position = 0; Util.DumpStream (pack_stream); Util.Log ("Unpack"); pack_stream.Position = 0; SpStream unpack_stream = new SpStream (); SpPacker.Unpack (pack_stream, unpack_stream); unpack_stream.Position = 0; Util.DumpStream (unpack_stream); Util.Log ("Decode"); unpack_stream.Position = 0; newObj = manager.Codec.Decode ("AddressBook", unpack_stream); CheckObj (newObj); }
public static SpStream Pack(SpStream input) { SpStream stream = new SpStream(); int pos = input.Position; if (Pack(input, stream) == false) { int size = stream.Position; size = ((size + 7) / 8) * 8; stream = new SpStream(size); input.Position = pos; // do we need this check? it's not possible it will return false this time if (Pack(input, stream) == false) { return(null); } } return(stream); }
public SpStream Response(int session, SpObject args) { SpObject header = new SpObject(); header.Insert("session", session); SpStream encode_stream = new SpStream(); mHostTypeManager.Codec.Encode(mHeaderType, header, encode_stream); if (session != 0 && mSessions.ContainsKey(session)) { mHostTypeManager.Codec.Encode(mSessions[session].Response, args, encode_stream); } SpStream pack_stream = new SpStream(); encode_stream.Position = 0; SpPacker.Pack(encode_stream, pack_stream); pack_stream.Position = 0; return(pack_stream); }
private void TestStr (string s) { SpObject obj = new SpObject (SpObject.ArgType.Table, "a", s); Util.Log ("------------------TestStr----------------------------"); Util.Log (s); Util.Log ("Encode"); SpStream encode_stream = new SpStream (2); bool ret = manager.Codec.Encode ("ss", obj, encode_stream); Util.Assert (ret == false); encode_stream = new SpStream (encode_stream.Position); ret = manager.Codec.Encode ("ss", obj, encode_stream); Util.Assert (ret == true); encode_stream.Position = 0; Util.DumpStream (encode_stream); Util.Log ("Pack"); encode_stream.Position = 0; SpStream pack_stream = new SpStream (); SpPacker.Pack (encode_stream, pack_stream); pack_stream.Position = 0; Util.DumpStream (pack_stream); Util.Log ("Unpack"); pack_stream.Position = 0; SpStream unpack_stream = new SpStream (); SpPacker.Unpack (pack_stream, unpack_stream); unpack_stream.Position = 0; Util.DumpStream (unpack_stream); Util.Log ("Decode"); unpack_stream.Position = 0; SpObject dobj = manager.Codec.Decode ("ss", unpack_stream); string ds = dobj["a"].AsString (); Util.Log (ds); Util.Assert (s == ds); }
public void Recv (IAsyncResult ar) { int ret = mSocket.EndReceive (ar); if (ret > 0) { mRecvOffset += ret; int read = 0; while (mRecvOffset > read) { int size = (mRecvBuffer[read + 1] | (mRecvBuffer[read + 0] << 8)); read += 2; if (mRecvOffset >= size + read) { SpStream stream = new SpStream (mRecvBuffer, read, size, size); SpRpcResult result = mRpc.Dispatch (stream); switch (result.Op) { case SpRpcOp.Request: Util.Log ("Recv Request : " + result.Protocol.Name + ", session : " + result.Session); if (result.Arg != null) Util.DumpObject (result.Arg); break; case SpRpcOp.Response: Util.Log ("Recv Response : " + result.Protocol.Name + ", session : " + result.Session); if (result.Arg != null) Util.DumpObject (result.Arg); break; } read += size; } } Util.Assert (mRecvOffset == read); mRecvOffset = 0; } mSocket.BeginReceive (mRecvBuffer, 0, mRecvBuffer.Length, SocketFlags.None, new System.AsyncCallback(ReadCallback), this); }
public static SpRpcResult Parse(byte[] bytes, int dataLength) { var stream = new SpStream(bytes, 0, dataLength, dataLength); return(_rpc.Dispatch(stream)); }
public static bool Unpack (SpStream input, SpStream output) { int src_size = input.Tail; int src_offset = input.Offset; byte[] src = input.Buffer; while (src_offset < src_size) { byte header = src[src_offset]; src_offset++; if (header == 0xff) { if (src_offset > src_size) return false; int n = (src[src_offset] + 1) * 8; if (src_size - src_offset < n + 1) return false; src_offset++; output.Write (src, src_offset, n); src_offset += n; } else { for (int i = 0; i < 8; i++) { int nz = (header >> i) & 1; if (nz != 0) { if (src_offset > src_size) return false; output.Write (src[src_offset]); src_offset++; } else { output.Write ((byte)0); } } } } return (output.IsOverflow () == false); }
private void TestNest () { SpObject clist = new SpObject (SpObject.ArgType.Table, "character", new SpObject (SpObject.ArgType.Array, new SpObject (SpObject.ArgType.Table, "id", 1, "attribute", new SpObject (SpObject.ArgType.Table, "level", 1, "templateId", 1001, "ability", new SpObject (SpObject.ArgType.Table, "hp", 2530, "att", 2310 ) ) ), new SpObject (SpObject.ArgType.Table, "id", 2, "attribute", new SpObject (SpObject.ArgType.Table, "level", 1, "templateId", 1002, "ability", new SpObject (SpObject.ArgType.Table, "hp", 1320, "att", 2090 ) ) ) ) ); Util.Log ("------------------TEST CHARACTER----------------------------"); Util.DumpObject (clist); Util.Log ("Encode"); SpStream encode_stream = new SpStream (); manager.Codec.Encode ("clist", clist, encode_stream); encode_stream.Position = 0; Util.DumpStream (encode_stream); Util.Log ("Pack"); encode_stream.Position = 0; SpStream pack_stream = new SpStream (); SpPacker.Pack (encode_stream, pack_stream); pack_stream.Position = 0; Util.DumpStream (pack_stream); Util.Log ("Unpack"); pack_stream.Position = 0; SpStream unpack_stream = new SpStream (); SpPacker.Unpack (pack_stream, unpack_stream); unpack_stream.Position = 0; Util.DumpStream (unpack_stream); Util.Log ("Decode"); unpack_stream.Position = 0; SpObject dobj = manager.Codec.Decode ("clist", unpack_stream); Util.DumpObject (dobj); }
public bool Encode (string proto, SpObject obj, SpStream stream) { return Encode (mTypeManager.GetType (proto), obj, stream); }
public static bool Pack (SpStream input, SpStream output) { int src_size = input.Length; if (src_size % 8 != 0) { int new_size = ((src_size + 7) / 8) * 8; if (input.Capacity < new_size) { SpStream new_input = new SpStream (new_size); Array.Copy (input.Buffer, input.Offset, new_input.Buffer, new_input.Offset, input.Length); new_input.Position = new_input.Offset; input = new_input; src_size = new_size; } else { int pos = input.Position; input.Position = input.Tail; for (int i = src_size; i < new_size; i++) { input.Write ((byte)0); } input.Position = pos; } } int src_start = input.Offset; int src_offset = input.Offset; byte[] src = input.Buffer; int ff_n = 0; int ff_src_start = 0; int ff_dest_start = 0; for (; src_offset < src_size; src_offset += 8) { int pos = output.Position; int n = PackSeg (src, src_offset, output, ff_n); if (n == 10) { ff_src_start = src_offset; ff_dest_start = pos; ff_n = 1; } else if (n == 8 && ff_n > 0) { ff_n++; if (ff_n == 256) { output.Position = ff_dest_start; WriteFF (src, ff_src_start, output, 256 * 8, n); ff_n = 0; } } else { if (ff_n > 0) { output.Position = ff_dest_start; WriteFF (src, ff_src_start, output, ff_n * 8, n); ff_n = 0; } } output.Position = pos + n; } if (ff_n == 1) { output.Position = ff_dest_start; WriteFF (src, ff_src_start, output, 8, 0); } else if (ff_n > 1) { output.Position = ff_dest_start; WriteFF (src, ff_src_start, output, src_size - (ff_src_start - src_start), 0); } return (output.IsOverflow () == false); }
public SpObject Decode (SpType type, SpStream stream) { if (type == null || stream == null) return null; mStream = stream; return DecodeInternal (type); }
public SpObject Decode (string proto, SpStream stream) { return Decode (mTypeManager.GetType (proto), stream); }
private SpStream EncodeRequest (string proto, SpObject args, int session) { if (mAttachTypeManager == null || mHostTypeManager == null || mHeaderType == null) return null; SpProtocol protocol = mAttachTypeManager.GetProtocolByName (proto); if (protocol == null) return null; SpObject header = new SpObject (); header.Insert ("type", protocol.Tag); if (session != 0) header.Insert ("session", session); SpStream stream = mHostTypeManager.Codec.Encode (mHeaderType, header); if (stream == null) return null; if (args != null) { if (mAttachTypeManager.Codec.Encode (protocol.Request, args, stream) == false) { if (stream.IsOverflow ()) { if (stream.Position > SpCodec.MAX_SIZE) return null; int size = stream.Position; size = ((size + 7) / 8) * 8; stream = new SpStream (size); if (mAttachTypeManager.Codec.Encode (protocol.Request, args, stream) == false) return null; } else { return null; } } } if (session != 0) { mSessions[session] = protocol; } return stream; }
public void Run() { SpTypeManager manager = LoadProto(); // different ways to create SpObject Util.Log("Object Create Check"); CheckObj(CreateObject()); CheckObj(CreateObject2()); SpObject obj = CreateObject3(); CheckObj(obj); Util.Log("Encode"); SpStream small_stream = new SpStream(32); bool success = manager.Codec.Encode("foobar", obj, small_stream); Util.Assert(success == false); Util.Log("encode failed! require size : " + small_stream.Position); small_stream.Position = 0; Util.DumpStream(small_stream); SpStream encode_stream = manager.Codec.Encode("foobar", obj); encode_stream.Position = 0; Util.DumpStream(encode_stream); Util.Log("Decode "); encode_stream.Position = 0; SpObject ooo = manager.Codec.Decode("foobar", encode_stream); CheckObj(ooo); Util.Log("Pack"); encode_stream.Position = 0; small_stream.Position = 0; success = SpPacker.Pack(encode_stream, small_stream); Util.Assert(success == false); Util.Log("pack failed! require size : " + small_stream.Position); small_stream.Position = 0; Util.DumpStream(small_stream); SpStream pack_stream = SpPacker.Pack(encode_stream); pack_stream.Position = 0; Util.DumpStream(pack_stream); Util.Log("Unpack"); pack_stream.Position = 0; small_stream.Position = 0; success = SpPacker.Unpack(pack_stream, small_stream); Util.Assert(success == false); Util.Log("unpack failed! require size : " + small_stream.Position); small_stream.Position = 0; Util.DumpStream(small_stream); pack_stream.Position = 0; SpStream decode_stream = SpPacker.Unpack(pack_stream); decode_stream.Position = 0; Util.DumpStream(decode_stream); Util.Log("Decode "); decode_stream.Position = 0; SpObject newObj = manager.Codec.Decode("foobar", decode_stream); CheckObj(newObj); }
public static SpStream Unpack (SpStream input) { SpStream stream = new SpStream (); int pos = input.Position; if (Unpack (input, stream) == false) { stream = new SpStream (stream.Position); input.Position = pos; if (Unpack (input, stream) == false) return null; } return stream; }
public bool Encode(string proto, SpObject obj, SpStream stream) { return(Encode(mTypeManager.GetType(proto), obj, stream)); }
public bool Request (string proto, SpObject args, int session, SpStream stream) { SpStream encode_stream = EncodeRequest (proto, args, session); encode_stream.Position = 0; return SpPacker.Pack (encode_stream, stream); }
public static bool Pack(SpStream input, SpStream output) { int src_size = input.Length; if (src_size % 8 != 0) { int new_size = ((src_size + 7) / 8) * 8; if (input.Capacity < new_size) { SpStream new_input = new SpStream(new_size); Array.Copy(input.Buffer, input.Offset, new_input.Buffer, new_input.Offset, input.Length); new_input.Position = new_input.Offset; input = new_input; src_size = new_size; } else { int pos = input.Position; input.Position = input.Tail; for (int i = src_size; i < new_size; i++) { input.Write((byte)0); } input.Position = pos; } } int src_start = input.Offset; int src_offset = input.Offset; byte[] src = input.Buffer; int ff_n = 0; int ff_src_start = 0; int ff_dest_start = 0; for (; src_offset < src_size; src_offset += 8) { int pos = output.Position; int n = PackSeg(src, src_offset, output, ff_n); if (n == 10) { ff_src_start = src_offset; ff_dest_start = pos; ff_n = 1; } else if (n == 8 && ff_n > 0) { ff_n++; if (ff_n == 256) { output.Position = ff_dest_start; WriteFF(src, ff_src_start, output, 256 * 8, n); ff_n = 0; } } else { if (ff_n > 0) { output.Position = ff_dest_start; WriteFF(src, ff_src_start, output, ff_n * 8, n); ff_n = 0; } } output.Position = pos + n; } if (ff_n == 1) { output.Position = ff_dest_start; WriteFF(src, ff_src_start, output, 8, 0); } else if (ff_n > 1) { output.Position = ff_dest_start; WriteFF(src, ff_src_start, output, src_size - (ff_src_start - src_start), 0); } return(output.IsOverflow() == false); }
public SpObject Decode(string proto, SpStream stream) { return(Decode(mTypeManager.GetType(proto), stream)); }
private void TestNest() { SpObject clist = new SpObject(SpObject.ArgType.Table, "character", new SpObject(SpObject.ArgType.Array, new SpObject(SpObject.ArgType.Table, "id", 1, "attribute", new SpObject(SpObject.ArgType.Table, "level", 1, "templateId", 1001, "ability", new SpObject(SpObject.ArgType.Table, "hp", 2530, "att", 2310 ) ) ), new SpObject(SpObject.ArgType.Table, "id", 2, "attribute", new SpObject(SpObject.ArgType.Table, "level", 1, "templateId", 1002, "ability", new SpObject(SpObject.ArgType.Table, "hp", 1320, "att", 2090 ) ) ) ) ); Util.Log("------------------TEST CHARACTER----------------------------"); Util.DumpObject(clist); Util.Log("Encode"); SpStream encode_stream = new SpStream(); manager.Codec.Encode("clist", clist, encode_stream); encode_stream.Position = 0; Util.DumpStream(encode_stream); Util.Log("Pack"); encode_stream.Position = 0; SpStream pack_stream = new SpStream(); SpPacker.Pack(encode_stream, pack_stream); pack_stream.Position = 0; Util.DumpStream(pack_stream); Util.Log("Unpack"); pack_stream.Position = 0; SpStream unpack_stream = new SpStream(); SpPacker.Unpack(pack_stream, unpack_stream); unpack_stream.Position = 0; Util.DumpStream(unpack_stream); Util.Log("Decode"); unpack_stream.Position = 0; SpObject dobj = manager.Codec.Decode("clist", unpack_stream); Util.DumpObject(dobj); }
private SpStream EncodeRequest(string proto, SpObject args, int session) { if (mAttachTypeManager == null || mHostTypeManager == null || mHeaderType == null) { return(null); } SpProtocol protocol = mAttachTypeManager.GetProtocolByName(proto); if (protocol == null) { return(null); } SpObject header = new SpObject(); header.Insert("type", protocol.Tag); if (session != 0) { header.Insert("session", session); } SpStream stream = mHostTypeManager.Codec.Encode(mHeaderType, header); if (stream == null) { return(null); } if (args != null) { if (mAttachTypeManager.Codec.Encode(protocol.Request, args, stream) == false) { if (stream.IsOverflow()) { if (stream.Position > SpCodec.MAX_SIZE) { return(null); } int size = stream.Position; size = ((size + 7) / 8) * 8; stream = new SpStream(size); if (mAttachTypeManager.Codec.Encode(protocol.Request, args, stream) == false) { return(null); } } else { return(null); } } } if (session != 0) { mSessions[session] = protocol; } return(stream); }
public SpRpcResult Dispatch (SpStream stream) { SpStream unpack_stream = SpPacker.Unpack (stream); unpack_stream.Position = 0; SpObject header = mHostTypeManager.Codec.Decode (mHeaderType, unpack_stream); int session = 0; if (header["session"] != null) session = header["session"].AsInt (); if (header["type"] != null) { // handle request SpProtocol protocol = mHostTypeManager.GetProtocolByTag (header["type"].AsInt ()); SpObject obj = mHostTypeManager.Codec.Decode (protocol.Request, unpack_stream); if (session != 0) mSessions[session] = protocol; return new SpRpcResult (session, protocol, SpRpcOp.Request, obj); } else { // handle response if (mSessions.ContainsKey (session) == false) return new SpRpcResult (); SpProtocol protocol = mSessions[session]; mSessions.Remove (session); if (protocol == null) return new SpRpcResult (); if (protocol.Response == null) return new SpRpcResult (session, protocol, SpRpcOp.Response, null); SpObject obj = mAttachTypeManager.Codec.Decode (protocol.Response, unpack_stream); return new SpRpcResult (session, protocol, SpRpcOp.Response, obj); } }
public SpStream Response (int session, SpObject args) { SpObject header = new SpObject (); header.Insert ("session", session); SpStream encode_stream = new SpStream (); mHostTypeManager.Codec.Encode (mHeaderType, header, encode_stream); if (session != 0 && mSessions.ContainsKey (session)) { mHostTypeManager.Codec.Encode (mSessions[session].Response, args, encode_stream); } SpStream pack_stream = new SpStream (); encode_stream.Position = 0; SpPacker.Pack (encode_stream, pack_stream); pack_stream.Position = 0; return pack_stream; }