void SendRCONPacket(RCONPacket p) { byte[] Packet = p.OutputAsBytes(); if (S.Connected) { S.BeginSend(Packet, 0, Packet.Length, SocketFlags.None, new AsyncCallback(SendCallback), this); } }
public bool Connect(IPEndPoint Server, string password) { try { IAsyncResult ar = null; try { ar = S.BeginConnect(Server, null, null); } catch (ObjectDisposedException) { S = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); PacketCount = 0; #if DEBUG TempPackets = new ArrayList(); #endif return(false); } ar.AsyncWaitHandle.WaitOne(2000); // wait 2 seconds if (!ar.IsCompleted) { OnError(ConnectionTimeout); S.Close(); return(false); } else // WAIT IS COMPLETED - if not connectsuccess (triggered elsewhere from sending auth packet), don't do forced timeout close { RCONPacket SA = new RCONPacket(); SA.RequestId = 1; SA.String1 = password; SA.ServerDataSent = RCONPacket.SERVERDATA_sent.SERVERDATA_AUTH; SendRCONPacket(SA); // auth packet if (S.Connected) // { StartGetNewPacket(); // This is the first time we've sent, so we can start listening now! return(true); } else { OnError(UnknownConnectionIssue); // idk what exactly S.Close(); return(false); } } //S.Connect(Server); } catch (SocketException) { OnError(ConnectionFailedString); OnConnectionSuccess(false); return(false); } }
public void ServerCommand(string command) { if (connected) { RCONPacket PacketToSend = new RCONPacket(); PacketToSend.RequestId = 2; PacketToSend.ServerDataSent = RCONPacket.SERVERDATA_sent.SERVERDATA_EXECCOMMAND; PacketToSend.String1 = command; SendRCONPacket(PacketToSend, command); //OnCommandOutput(command); } }
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 Console.WriteLine("Complete packet."); #endif RCONPacket RetPack = new RCONPacket(); if (state.Data.Length != 0) { RetPack.ParseFromBytes(state.Data, this); ProcessResponse(RetPack); // Wait for new packet. if (S.Connected) { StartGetNewPacket(); } } else { //Console.WriteLine("KICKED TIMEOUT DATA:" + UTF8Encoding.UTF8.GetString(state.Data)); OnError(ConnectionClosed); S.Close(); } return; } } }
void ProcessResponse(RCONPacket P) { switch (P.ServerDataReceived) { case RCONPacket.SERVERDATA_rec.SERVERDATA_AUTH_RESPONSE: if (P.RequestId != -1) { // Connected. connected = true; OnSuccessOutput(ConnectionSuccessString); OnConnectionSuccess(true); } else { // Failed! OnError(WrongPassword); OnConnectionSuccess(false); S.Close(); } break; case RCONPacket.SERVERDATA_rec.SERVERDATA_RESPONSE_VALUE: if (hadjunkpacket) { // Real packet! OnServerOutput(P.String1); } else { hadjunkpacket = true; //OnError(GotJunkPacket); } break; default: OnError(UnknownResponseType); break; } }