void ParseReceivePacket() { //TODO: review here, optimized buffer _receiveBuffer = new byte[DEFAULT_BUFFER_SIZE]; var socket = _conn.socket; int receive = socket.Receive(_receiveBuffer); if (receive == 0) { return; } //--------------------------------------------------- //TODO: review err handling _parser.LoadNewBuffer(_receiveBuffer, receive); switch (_receiveBuffer[4]) { case ERROR_CODE: LoadError = new ErrPacket(); LoadError.ParsePacket(_parser); break; case 0xfe: //0x00 or 0xfe the OK packet header case OK_CODE: OkPacket = new OkPacket(_conn.IsProtocol41); OkPacket.ParsePacket(_parser); break; default: ParseResultSet(); break; } }
public void Connect() { if (State == ConnectionState.Connected) { throw new NotSupportedException("already connected"); } var endpoint = new IPEndPoint(IPAddress.Parse(config.host), config.port); socket.Connect(endpoint); byte[] buffer = new byte[512]; int count = socket.Receive(buffer); if (count > 0) { _writer.Reset(); _parser.LoadNewBuffer(buffer, count); _handshake = new HandshakePacket(); _handshake.ParsePacket(_parser); threadId = _handshake.threadId; byte[] token = MakeToken(config.password, GetScrollbleBuffer(_handshake.scrambleBuff1, _handshake.scrambleBuff2)); _writer.IncrementPacketNumber(); //------------------------------------------ var authPacket = new ClientAuthenticationPacket(); authPacket.SetValues(config.user, token, config.database, _handshake.protocol41); authPacket.WritePacket(_writer); byte[] sendBuff = _writer.ToArray(); byte[] receiveBuff = new byte[512]; //------------------------------------------- //send data int sendNum = socket.Send(sendBuff); int receiveNum = socket.Receive(receiveBuff); _parser.LoadNewBuffer(receiveBuff, receiveNum); if (receiveBuff[4] == 255) { ErrPacket errPacket = new ErrPacket(); errPacket.ParsePacket(_parser); return; } else { OkPacket okPacket = new OkPacket(_handshake.protocol41); okPacket.ParsePacket(_parser); } _writer.Reset(); GetMaxAllowedPacket(); _writer.SetMaxAllowedPacket(_maxPacketSize); } }