public static System.Object PacketToProtocol(ushort packetId, Packet packet) { switch (packetId) { case 1510: ReqChatSend reqChatSend = new ReqChatSend(packet); return(reqChatSend); case 1530: ReqChatGm reqChatGm = new ReqChatGm(packet); return(reqChatGm); case 1010: ReqRoleLogin reqRoleLogin = new ReqRoleLogin(packet); return(reqRoleLogin); case 1020: ReqRoleCreate reqRoleCreate = new ReqRoleCreate(packet); return(reqRoleCreate); case 1030: ReqRoleRandName reqRoleRandName = new ReqRoleRandName(packet); return(reqRoleRandName); case 2010: ReqSceneEnterFly reqSceneEnterFly = new ReqSceneEnterFly(packet); return(reqSceneEnterFly); case 2020: ReqSceneEnter reqSceneEnter = new ReqSceneEnter(packet); return(reqSceneEnter); case 2030: ReqSceneMove reqSceneMove = new ReqSceneMove(packet); return(reqSceneMove); case 2070: ReqSceneReqPlayers reqSceneReqPlayers = new ReqSceneReqPlayers(packet); return(reqSceneReqPlayers); case 40010: ReqTestSend reqTestSend = new ReqTestSend(packet); return(reqTestSend); case 40040: ReqTestXX reqTestXX = new ReqTestXX(packet); return(reqTestXX); case 40060: ReqTestPhp reqTestPhp = new ReqTestPhp(packet); return(reqTestPhp); } return(null); }
public static System.Object PacketToProtocol(ushort packetId, Packet packet) { switch (packetId) { case 1510: ReqChatSend reqChatSend = new ReqChatSend(packet); return reqChatSend; case 1530: ReqChatGm reqChatGm = new ReqChatGm(packet); return reqChatGm; case 1010: ReqRoleLogin reqRoleLogin = new ReqRoleLogin(packet); return reqRoleLogin; case 1020: ReqRoleCreate reqRoleCreate = new ReqRoleCreate(packet); return reqRoleCreate; case 1030: ReqRoleRandName reqRoleRandName = new ReqRoleRandName(packet); return reqRoleRandName; case 2010: ReqSceneEnterFly reqSceneEnterFly = new ReqSceneEnterFly(packet); return reqSceneEnterFly; case 2020: ReqSceneEnter reqSceneEnter = new ReqSceneEnter(packet); return reqSceneEnter; case 2030: ReqSceneMove reqSceneMove = new ReqSceneMove(packet); return reqSceneMove; case 2070: ReqSceneReqPlayers reqSceneReqPlayers = new ReqSceneReqPlayers(packet); return reqSceneReqPlayers; case 40010: ReqTestSend reqTestSend = new ReqTestSend(packet); return reqTestSend; case 40040: ReqTestXX reqTestXX = new ReqTestXX(packet); return reqTestXX; case 40060: ReqTestPhp reqTestPhp = new ReqTestPhp(packet); return reqTestPhp; } return null; }
private static void DoMsg(Socket clientSocket, ushort packetid, Packet packet) { Console.WriteLine("packetid: {0}", packetid); switch (packetid) { case 40040: // ReqTestXX reqTestXX = new ReqTestXX(packet); ReqTestXX reqTestXX = P2P.PacketToProtocol(packetid, packet) as ReqTestXX; Console.WriteLine("{0} {1} {2} {3} {4}", reqTestXX.id_u8, reqTestXX.id_u16, reqTestXX.id_u32, reqTestXX.repeat_id_u8, reqTestXX.optional_id_u8); AckTestXX ackTestXX = new AckTestXX(); ackTestXX.id_u8 = 111; ackTestXX.id_u16 = 11111; ackTestXX.id_u32 = 1111111; ackTestXX.optional_id_u8 = 222; ackTestXX.repeat_id_u8 = new List <byte>(); clientSocket.Send(ackTestXX.Encode().GetBufferBytes()); break; } }
static void Main(string[] args) { IPAddress ip = IPAddress.Parse("127.0.0.1"); Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); clientSocket.Connect(new IPEndPoint(ip, 8888)); Console.WriteLine("连接服务器成功"); ReqTestXX reqTestXX = new ReqTestXX(); reqTestXX.id_u8 = 111; reqTestXX.id_u16 = 11111; reqTestXX.id_u32 = 1111111; reqTestXX.optional_id_u8 = 222; reqTestXX.repeat_id_u8 = new List <byte>(); Console.WriteLine("reqTestXX.id_u8 : {0}", reqTestXX.id_u8); Packet xx = reqTestXX.Encode(); Console.WriteLine("reqTestXX.id_u16 : {0}", reqTestXX.id_u16); byte[] oo = xx.GetBufferBytes(); clientSocket.Send(oo); ushort lenHeader = 2; uint lenMaxBuff = 51200; ushort lenTmpBuff = 512; byte[] buffers = new byte[lenMaxBuff]; byte[] buffersTmp = new byte[lenTmpBuff]; int buffersLen = 0; while (true) { int readCount = clientSocket.Receive(buffersTmp, lenTmpBuff, 0); if (readCount == 0) { break; } Array.Copy(buffersTmp, 0, buffers, buffersLen, readCount); buffersLen += readCount; if (buffersLen <= lenHeader) { continue; } while (true) { ushort packageCount = PacketUtil.ReadUshort(buffers); if (buffersLen < packageCount + lenHeader) { break; } buffersLen -= packageCount + lenHeader; byte[] buffPid = new byte[2]; byte[] buff = new byte[packageCount]; Array.Copy(buffers, 2, buffPid, 0, 2); Array.Copy(buffers, 4, buff, 0, packageCount - 2); Array.Copy(buffers, packageCount + 2, buffers, 0, buffersLen); ushort packetId = PacketUtil.ReadUshort(buffPid); Packet packet = new Packet(buff); DoMsg(clientSocket, packetId, packet); } } clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); }
static void Main(string[] args) { IPAddress ip = IPAddress.Parse("127.0.0.1"); Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); clientSocket.Connect(new IPEndPoint(ip, 8888)); Console.WriteLine("连接服务器成功"); ReqTestXX reqTestXX = new ReqTestXX(); reqTestXX.id_u8 = 111; reqTestXX.id_u16 = 11111; reqTestXX.id_u32 = 1111111; reqTestXX.optional_id_u8 = 222; reqTestXX.repeat_id_u8=new List<byte>(); Console.WriteLine("reqTestXX.id_u8 : {0}", reqTestXX.id_u8); Packet xx = reqTestXX.Encode(); Console.WriteLine("reqTestXX.id_u16 : {0}", reqTestXX.id_u16); byte[] oo = xx.GetBufferBytes(); clientSocket.Send(oo); ushort lenHeader = 2; uint lenMaxBuff = 51200; ushort lenTmpBuff = 512; byte[] buffers = new byte[lenMaxBuff]; byte[] buffersTmp = new byte[lenTmpBuff]; int buffersLen = 0; while (true) { int readCount = clientSocket.Receive(buffersTmp, lenTmpBuff, 0); if (readCount == 0) { break; } Array.Copy(buffersTmp, 0, buffers, buffersLen, readCount); buffersLen += readCount; if (buffersLen <= lenHeader) { continue; } while (true) { ushort packageCount = PacketUtil.ReadUshort(buffers); if (buffersLen < packageCount + lenHeader) { break; } buffersLen -= packageCount + lenHeader; byte[] buffPid = new byte[2]; byte[] buff = new byte[packageCount]; Array.Copy(buffers, 2, buffPid, 0, 2); Array.Copy(buffers, 4, buff, 0, packageCount - 2); Array.Copy(buffers, packageCount + 2, buffers, 0, buffersLen); ushort packetId = PacketUtil.ReadUshort(buffPid); Packet packet = new Packet(buff); DoMsg(clientSocket, packetId, packet); } } clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); }