public bool Connect(IPEndPoint Server, string password) { try { S.Connect(Server); } catch (SocketException) { OnError(ConnectionFailedString); OnConnectionSuccess(false); return(false); } RCONPacket SA = new RCONPacket(); SA.RequestId = 1; SA.String1 = password; SA.ServerDataSent = RCONPacket.SERVERDATA_sent.SERVERDATA_AUTH; SendRCONPacket(SA); // This is the first time we've sent, so we can start listening now! StartGetNewPacket(); return(true); }
public void ServerCommand(string command) { if (connected) { RCONPacket PacketToSend = new RCONPacket(); PacketToSend.RequestId = 2; PacketToSend.ServerDataSent = RCONPacket.SERVERDATA_sent.SERVERDATA_EXECCOMMAND; PacketToSend.String1 = command; this.commandName = GetCommandName(command); // This is stored in state so the command name can be returned with the response SendRCONPacket(PacketToSend); } }
void ProcessResponse(RCONPacket P) { switch (P.ServerDataReceived) { case RCONPacket.SERVERDATA_rec.SERVERDATA_AUTH_RESPONSE: if (P.RequestId != -1) { // Connected. connected = true; OnError(ConnectionSuccessString); OnConnectionSuccess(true); } else { // Failed! OnError(ConnectionFailedString); OnConnectionSuccess(false); } break; case RCONPacket.SERVERDATA_rec.SERVERDATA_RESPONSE_VALUE: if (hadjunkpacket) { // Real packet! OnServerOutput(this.commandName, P.String1); } else { hadjunkpacket = true; //OnError(GotJunkPacket); OnError(string.Empty); } break; default: OnError(UnknownResponseType); break; } }
void ProcessIncomingData(RecState state) { if (state.IsPacketLength) { // First 4 bytes of a new packet. state.PacketLength = BitConverter.ToInt32(state.Data, 0); state.IsPacketLength = false; state.BytesSoFar = 0; state.Data = new byte[state.PacketLength]; S.BeginReceive(state.Data, 0, state.PacketLength, SocketFlags.None, new AsyncCallback(ReceiveCallback), state); } else { // Do something with data... if (state.BytesSoFar < state.PacketLength) { // Missing data. S.BeginReceive(state.Data, state.BytesSoFar, state.PacketLength - state.BytesSoFar, SocketFlags.None, new AsyncCallback(ReceiveCallback), state); } else { // Process data. #if DEBUG Trace.WriteLine("Complete packet."); #endif RCONPacket RetPack = new RCONPacket(); RetPack.ParseFromBytes(state.Data, this); ProcessResponse(RetPack); // Wait for new packet. StartGetNewPacket(); } } }
void SendRCONPacket(RCONPacket p) { byte[] Packet = p.OutputAsBytes(); S.BeginSend(Packet, 0, Packet.Length, SocketFlags.None, new AsyncCallback(SendCallback), this); }