private void Read_Callback(System.IAsyncResult ar) { StyxPacketInfo packetInfo = (StyxPacketInfo)ar.AsyncState; Socket s = packetInfo.socket; int read = s.EndReceive(ar); if (read > 0) { if (packetInfo.state == StyxPacketInfo.STATE.header) { packetInfo.readHeader(); packetInfo.state = StyxPacketInfo.STATE.packet; s.BeginReceive(packetInfo.packetBuffer, StyxPacketInfo.HEADER_SIZE, packetInfo.packetLength - StyxPacketInfo.HEADER_SIZE, 0, new System.AsyncCallback(Read_Callback), packetInfo); } else { packetInfo.bytesLeft -= read; if (packetInfo.bytesLeft == 0) { handlePacketBuffer(packetInfo.packetBuffer); StyxPacketInfo newPacketInfo = new StyxPacketInfo(); newPacketInfo.socket = s; s.BeginReceive(newPacketInfo.headerBuffer, 0, StyxPacketInfo.HEADER_SIZE, 0, new System.AsyncCallback(Read_Callback), newPacketInfo); } else { s.BeginReceive(packetInfo.packetBuffer, packetInfo.packetLength - packetInfo.bytesLeft, packetInfo.bytesLeft, 0, new AsyncCallback(Read_Callback), packetInfo); } } } }
public void TestLogin() { connected.Reset(); socket.BeginConnect(HOST, PORT, new System.AsyncCallback(onConnect), socket); connected.WaitOne(); StyxPacketInfo packetInfo = new StyxPacketInfo(); packetInfo.socket = socket; socket.BeginReceive(packetInfo.headerBuffer, 0, StyxPacketInfo.HEADER_SIZE, 0, new System.AsyncCallback(Read_Callback), packetInfo); LoginRequestPacket loginRequestPacket = new LoginRequestPacket(); // TODO: doan nay cua mini var jsonUser = new JSONClass(); jsonUser ["userid"].AsInt = 2; jsonUser ["username"] = "******"; jsonUser ["gameid"].AsInt = gameId; loginRequestPacket.user = jsonUser.ToString(); loginRequestPacket.password = "******"; //login.pwd; loginRequestPacket.operatorid = operatorID; sendPacket(loginRequestPacket); }