private void SendPacket(RconPacketType type, Dictionary <string, string> data) { var str = JsonConvert.SerializeObject(new RconPacket(type, data)); var bytes = Encoding.UTF8.GetBytes(str); localSocket.Send(bytes); }
private RconResponse PrivateSendPacketAndGetResponse(RconPacketType type, string body, int timeoutMs = 900, bool reconnectOnFail = true) { //This is a bit gross. I should find a way around this. //We want to hang the thread while we search for the response. //If it takes too long, respond with a timeout. var task = Task.Run(() => PrivateTaskSendPacketAndGetResponse(type, body, timeoutMs)); if (task.Wait(TimeSpan.FromMilliseconds(timeoutMs))) { return(task.Result); } else { //Timeout. if (reconnectOnFail) { //Try to reconnect. DisposeNetworking(); PrepareNetworking(); Console.WriteLine("Reconnected"); //Resend command return(PrivateSendPacketAndGetResponse(type, body, timeoutMs, false)); } return(RconResponse.CreateBadResponse(RconResponseStatus.Timeout)); } }
public RconPacket(int _id, RconPacketType _type, string _body) { //Main constructor ID = _id; type = _type; body = _body; //This does NOT send the packet. }
public static RconPacket CreatePacket(int id, RconPacketType type, string body) { var packet = new RconPacket(); packet.ID = id; packet.Type = (int)type; packet.Body = body; return(packet); }
private void PrivateSendPacket(RconPacketType type, string body) { //Create the packet. RconPacket packet = new RconPacket(connectionID, type, body); //Get the byte data from our packet. byte[] rawData = packet.CreatePacket(); //Send it over the network stream networkWriter.Write(rawData); networkWriter.Flush(); //The data has been sent. }
//Private helper methods private void PrivateGetPackets(RconPacket input, GotPacketCallback callback) { //Get ID and incrememnt it int id = currentId++; int testId = currentId++;//This id will be used to check when we're done. input.id = id; //Register a callback for this and the next one. int packetsGot = 0; //How many packets were recievied for a single ID. List <RconPacket> returnedPackets = new List <RconPacket>(); var innerCallback = (new InternalPacketCallback((RconPacket p) => { //When a packet is received, this is called. //A request to the server will be sent using the same ID. When that packet is recieved, we know we are done. This is for getting multiple packets for one request if it is too long. RconPacketType type = (RconPacketType)p.type; bool isEnd = testId == p.id; //Check if the raw contents match the end of packet results if (packetsGot == 0) { //Send a request to check if we are done. sock.Send(new RconPacket(testId, RconPacketType.SERVERDATA_RESPONSE_VALUE, "").ToBytes()); } if (isEnd) { //This is the end. Return all of our packets. callback(returnedPackets); //Also remove this callback from the dictonary to save ram internalCallbacks.Remove(id); internalCallbacks.Remove(testId); } else { //Add this because it's not the ending packet. returnedPackets.Add(p); } packetsGot++; })); internalCallbacks.Add(id, innerCallback); internalCallbacks.Add(testId, innerCallback); //Send packet to the server. byte[] data = input.ToBytes(); //Send sock.Send(data); }
public static RconResponse CreateOkayResponse(RconPacketType _type, int _id, byte[] _body) { //Used when a response was correctly downloaded RconResponse rr = new RconResponse(); rr.type = _type; rr.id = _id; rr.body = Encoding.ASCII.GetString(_body); rr.status = RconResponseStatus.Ok; //If the message is "Keep Alive", then set the status. if (rr.body == "Keep Alive") { rr.status = RconResponseStatus.KeepAliveError; } return(rr); }
private async Task <RconResponse> PrivateTaskSendPacketAndGetResponse(RconPacketType type, string body, int timeoutMs) { //Clear ClearMainStream(); //Send PrivateSendPacket(type, body); DateTime start = DateTime.UtcNow; //Save the starting date so we can timeout. while (true) { //Keep trying to read from the stream. try { int responseLength = networkReader.ReadInt32(); //Fetch the packet length. Then, read in. int id = networkReader.ReadInt32(); //Read in the ID of the client that sent this. We should make sure this is ours. RconPacketType responseType = (RconPacketType)networkReader.ReadInt32(); //Fetch packet type //Read bytes in. byte[] buffer = networkReader.ReadBytes(responseLength - 10); //Read the body in. Use the length minus the header length of 10. //Read padding networkReader.Read(); networkReader.Read(); //Create a response. RconResponse response = RconResponse.CreateOkayResponse(responseType, id, buffer); //Return this return(response); } catch (Exception ex) { } //Some other error occured. Try again, but keep checking for a timeout. TimeSpan totalTime = DateTime.UtcNow - start; if (totalTime.TotalMilliseconds > timeoutMs) { //Timeout. Create bad response. return(RconResponse.CreateBadResponse(RconResponseStatus.Timeout)); } } }
public RconPacket(int requestId, RconPacketType type, byte[] payload) { RequestId = requestId; Type = type; Payload = payload; }
public RconPacket(RconConnection m_conn) { conn = m_conn; int size = ReadInt(); if (size > 4096) size = 4096; byte[] thispacket = ReadBytes(size); request_id = ReadInt(thispacket); size = ReadInt(thispacket); type = (RconPacketType)size; if (size != 2 && size != 3) { string1 = ""; string2 = ""; } else { string1 = ReadString(thispacket); string2 = ReadString(thispacket); } }
public RconPacket(RconConnection m_conn, RconPacketType m_type, int m_request_id, string m_string1, string m_string2) { conn = m_conn; type = m_type; request_id = m_request_id; string1 = m_string1; string2 = m_string2; }
public RconPacket(int _id, RconPacketType _type, string _body) { id = _id; body = _body; type = (int)_type; }
public RconPacket(RconPacketType type, Dictionary <string, string> data) { this.type = type; this.data = data; }