static void Main(string[] args) { RC4Encryption rc4Server = new RC4Encryption(); RC4Encryption rc4Client = new RC4Encryption(); rc4Server.makeSBox("zhangyawei"); rc4Client.makeSBox("zhangyawei"); TestIntegerData idata = new TestIntegerData('a', 1, 5, 20, 30, 40, 50, 60, 70); TestFloatData fdata = new TestFloatData(243.123123f, 32432.123); TestStringData sdata = new TestStringData("love"); EchoPack pack = new EchoPack(); pack._iarray = new Proto4z.TestIntegerDataArray(); pack._iarray.Add(idata); pack._iarray.Add(idata); pack._iarray.Add(idata); pack._farray = new Proto4z.TestFloatDataArray(); pack._farray.Add(fdata); pack._farray.Add(fdata); pack._farray.Add(fdata); pack._sarray = new Proto4z.TestStringDataArray(); pack._sarray.Add(sdata); pack._sarray.Add(sdata); pack._sarray.Add(sdata); pack._imap = new Proto4z.TestIntegerDataMap(); pack._imap.Add("123", idata); pack._imap.Add("223", idata); pack._fmap = new Proto4z.TestFloatDataMap(); pack._fmap.Add("523", fdata); pack._fmap.Add("623", fdata); pack._smap = new Proto4z.TestStringDataMap(); pack._smap.Add("723", sdata); pack._smap.Add("823", sdata); var now = DateTime.UtcNow; for (int i = 0; i < 1; i++) { var byteData = pack.__encode(); var binData = byteData.ToArray(); // for (int i = 0; i < binData.Length; i++) // { // System.Console.WriteLine((int)binData[i]); // } rc4Server.encryption(binData, binData.Length); rc4Client.encryption(binData, binData.Length); // for (int i = 0; i < binData.Length; i++) // { // System.Console.WriteLine((int)binData[i]); // } var v = new EchoPack(); int pos = 0; v.__decode(binData, ref pos); Client client = new Client(); client.Run(binData); } System.Console.WriteLine(DateTime.UtcNow - now); }
public void Run() { RC4Encryption rc4Server = new RC4Encryption(); RC4Encryption rc4Client = new RC4Encryption(); rc4Server.makeSBox("zhangyawei"); rc4Client.makeSBox("zhangyawei"); IPAddress ip = IPAddress.Parse("127.0.0.1"); Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { clientSocket.Connect(new IPEndPoint(ip, 26001)); Debug.logger.Log("connect Success."); } catch { Debug.logger.Log("connect Failed"); return; } if (true) { ClientAuthReq req = new ClientAuthReq("test", "123"); var binData = req.__encode().ToArray(); //binData = rc4Client.encryption(binData, binData.Length()); var sendData = new System.Collections.Generic.List <byte>(); NetHeader head = new NetHeader(); head.packLen = (UInt32)(4 + 2 + 2 + binData.Length); head.protoID = Proto4z.ClientAuthReq.getProtoID(); sendData.AddRange(head.__encode()); sendData.AddRange(binData); clientSocket.Send(sendData.ToArray()); var recvBytes = new byte[2000]; uint curLen = 0; uint needLen = 4 + 2 + 2; //暂时分两段读 后面要改buff接收提高效率 uint recvLen = 0; NetHeader recvHead = new NetHeader(); do { recvLen = (uint)clientSocket.Receive(recvBytes, (int)curLen, (int)needLen, System.Net.Sockets.SocketFlags.None);//第一段 if (recvLen == 0) { // remote close socket. return; } curLen += recvLen; needLen -= recvLen; if (needLen == 0 && curLen == 4 + 2 + 2) ////第一段 完成 { int pos = 0; recvHead.__decode(recvBytes, ref pos); needLen = recvHead.packLen - 4 - 2 - 2; //设置第二段 } else if (needLen == 0) //第二段完成 { if (recvHead.protoID == Proto4z.ClientAuthResp.getProtoID()) { ClientAuthResp result = new ClientAuthResp(); int pos = 4 + 2 + 2; result.__decode(recvBytes, ref pos); Debug.logger.Log("ClientAuthResp: account=" + result.account + ", token=" + result.token + ",retCode=" + result.retCode); int t = 0; t++; } else if (true) //other proto { } break; //一个协议接收处理完毕后break } recvLen = 0; } while (true); } }
static void Main(string[] args) { RC4Encryption rc4Server = new RC4Encryption(); RC4Encryption rc4Client = new RC4Encryption(); rc4Server.makeSBox("zhangyawei"); rc4Client.makeSBox("zhangyawei"); IntegerData idata = new IntegerData('a', 1, 5, 20, 30, 40, 50, 60); FloatData fdata = new FloatData(243.123123f, 32432.123); StringData sdata = new StringData("love"); EchoPack pack = new EchoPack(); pack._iarray = new IntegerDataArray(); pack._iarray.Add(idata); pack._iarray.Add(idata); pack._iarray.Add(idata); pack._farray = new FloatDataArray(); pack._farray.Add(fdata); pack._farray.Add(fdata); pack._farray.Add(fdata); pack._sarray = new StringDataArray(); pack._sarray.Add(sdata); pack._sarray.Add(sdata); pack._sarray.Add(sdata); pack._imap = new IntegerDataMap(); pack._imap.Add("123", idata); pack._imap.Add("223", idata); pack._fmap = new FloatDataMap(); pack._fmap.Add("523", fdata); pack._fmap.Add("623", fdata); pack._smap = new StringDataMap(); pack._smap.Add("723", sdata); pack._smap.Add("823", sdata); var now = DateTime.UtcNow; for (int i = 0; i < 1; i++) { var byteData = pack.__encode(); var binData = byteData.ToArray(); // for (int i = 0; i < binData.Length; i++) // { // System.Console.WriteLine((int)binData[i]); // } rc4Server.encryption(binData, binData.Length); rc4Client.encryption(binData, binData.Length); // for (int i = 0; i < binData.Length; i++) // { // System.Console.WriteLine((int)binData[i]); // } var v = new EchoPack(); int pos = 0; v.__decode(binData, ref pos); try { SimplePack pk = new SimplePack(10, "name", 100, null); pk.moneyTree = new MoneyTree(10001, 5, 5, 0, 0); var binMemory = pk.__encode().ToArray(); //序列化 SimplePack recvPK = new SimplePack(); int curPos = 0; recvPK.__decode(binMemory, ref curPos); System.Console.Write("success"); } catch (Exception e) { System.Console.Write("error"); } Client client = new Client(); client.Run(binData); } System.Console.WriteLine(DateTime.UtcNow - now); }