/************************************************************************/ /* 데이터를 파싱한다. * /************************************************************************/ public PacketHeader(MemoryStream ms) : this() { // 기본 해더 baseHeader = (byte)ms.ReadByte(); // 인크립스 encryt = (byte)ms.ReadByte(); // RequestID var requestIDBytes = new byte[20]; ms.Read(requestIDBytes, 0, requestIDBytes.Length); RequestID = Encoding.UTF8.GetString(requestIDBytes); var commandBuf = new byte[2]; ms.Read(commandBuf, 0, commandBuf.Length); var ohCommand = BitConverter.ToInt16(commandBuf, 0); ohCommand = IPAddress.NetworkToHostOrder(ohCommand); // 커멘드 처리 command = (PacketCommandType)ohCommand; // 데이터 타입 처리 dataType = (PacketDataType)ms.ReadByte(); // 성공 여부 처리 Success = (PacketSuccessType)ms.ReadByte(); }
public Packet(PacketCommandType x, IPAddress ip, string body) { PacketType = x; IPExternal = ip; PacketBody = body; }
private void StartReceive(object sender, DoWorkEventArgs e) { while (this.socket.Connected) // if socket connected { //Read packert Type. byte[] buffer = new byte[4]; int readBytes = this.NS.Read(buffer, 0, 4); if (readBytes == 0) { break; } PacketCommandType cmdType = (PacketCommandType)(BitConverter.ToInt32(buffer, 0)); //------------------------------------------------------------------------------------ // read Ip of reciver to confirm this is where its supposed to go string ipthis = ""; buffer = new byte[4]; readBytes = this.NS.Read(buffer, 0, 4); if (readBytes == 0) { break; } int ipSize = BitConverter.ToInt32(buffer, 0); buffer = new byte[ipSize]; readBytes = this.NS.Read(buffer, 0, ipSize); if (readBytes == 0) { break; } ipthis = System.Text.Encoding.ASCII.GetString(buffer); //------------------------------------------------------------------------------- // read Ip sender string ipofClient = ""; buffer = new byte[4]; readBytes = this.NS.Read(buffer, 0, 4); if (readBytes == 0) { break; } int ipThisSize = BitConverter.ToInt32(buffer, 0); buffer = new byte[ipSize]; readBytes = this.NS.Read(buffer, 0, ipThisSize); if (readBytes == 0) { break; } ipofClient = System.Text.Encoding.ASCII.GetString(buffer); //-------------------------------------------------------------------------------- //Read username string cmdUsername = ""; buffer = new byte[4]; readBytes = this.NS.Read(buffer, 0, 4); if (readBytes == 0) { break; } int sizeUsername = BitConverter.ToInt32(buffer, 0); //Read the command's Meta data. buffer = new byte[sizeUsername]; readBytes = this.NS.Read(buffer, 0, sizeUsername); if (readBytes == 0) { break; } cmdUsername = System.Text.Encoding.Unicode.GetString(buffer); //-------------------------------------------------------------------------------------------------- //Read the command's MetaData size. string rPacket = ""; buffer = new byte[4]; readBytes = this.NS.Read(buffer, 0, 4); if (readBytes == 0) { break; } int sizePacket = BitConverter.ToInt32(buffer, 0); //Read the command's Meta data. buffer = new byte[sizePacket]; readBytes = this.NS.Read(buffer, 0, sizePacket); if (readBytes == 0) { break; } rPacket = System.Text.Encoding.Unicode.GetString(buffer); // build packet--------------------------------------------------------------------------------------------------------- Packet cmd = new Packet(cmdType, IPAddress.Parse(ipofClient), rPacket); cmd.IPThisMachine = IPAddress.Parse(ipthis); cmd.Username = cmdUsername; this.OnCommandReceived(new PacketEA(cmd)); } this.OnDisconnected(new ClientEventArgs(this.socket)); this.Disconnect(); }