public bool Connect(IPEndPoint Server, string password) { try { S = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); PacketCount = 0; connected = false; S.Connect(Server); } catch { OnServerOutput("Error: " + 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 && S != null && S.Connected) { RCONPacket PacketToSend = new RCONPacket(); PacketToSend.RequestId = 2; PacketToSend.ServerDataSent = RCONPacket.SERVERDATA_sent.SERVERDATA_EXECCOMMAND; PacketToSend.String1 = command; SendRCONPacket(PacketToSend); } }
void SendRCONPacket(RCONPacket p) { try { byte[] Packet = p.OutputAsBytes(); S.BeginSend(Packet, 0, Packet.Length, SocketFlags.None, new AsyncCallback(SendCallback), this); } catch { OnServerOutput("Error: Failed to send packet"); } }
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]; try { S.BeginReceive(state.Data, 0, state.PacketLength, SocketFlags.None, new AsyncCallback(ReceiveCallback), state); } catch { OnServerOutput("Error: Failed BeginReceive"); } } else { // Do something with data... if (state.BytesSoFar < state.PacketLength) { try { // Missing data. S.BeginReceive(state.Data, state.BytesSoFar, state.PacketLength - state.BytesSoFar, SocketFlags.None, new AsyncCallback(ReceiveCallback), state); } catch { OnServerOutput("Error: Failed BeginReceive"); } } else { // Process data. #if DEBUG Console.WriteLine("Complete packet."); #endif RCONPacket RetPack = new RCONPacket(); RetPack.ParseFromBytes(state.Data, this); ProcessResponse(RetPack); // Wait for new packet. StartGetNewPacket(); } } }
void ProcessResponse(RCONPacket P) { switch (P.ServerDataReceived) { case RCONPacket.SERVERDATA_rec.SERVERDATA_AUTH_RESPONSE: if (P.RequestId != -1) { // Connected. connected = true; OnServerOutput(ConnectionSuccessString); OnConnectionSuccess(true); } else { // Failed! OnServerOutput("Error: " + ConnectionFailedString); OnConnectionSuccess(false); } break; case RCONPacket.SERVERDATA_rec.SERVERDATA_RESPONSE_VALUE: //if (hadjunkpacket) { // Real packet! OnServerOutput("Packet: " + P.String1); } // else { // hadjunkpacket = true; // OnServerOutput("Info: " + GotJunkPacket); } break; default: OnServerOutput("Error: " + UnknownResponseType); break; } }