public SOEReader(SOEPacket packet) { Stream = new MemoryStream(packet.GetRaw()); // Skip the SOE OpCode ReadUInt16(); }
public SOEWriter(SOEPacket packet) { // Message information Data = new List<byte>(packet.GetRaw()); OpCode = packet.GetOpCode(); // We're a packet, not a message IsMessage = false; }
public SOEWriter(SOEPacket packet) { // Message information Data = new List <byte>(packet.GetRaw()); OpCode = packet.GetOpCode(); // We're a packet, not a message IsMessage = false; }
public void HandleSessionRequest(SOEClient sender, SOEPacket packet) { // Setup a reader SOEReader reader = new SOEReader(packet); // Get the data from the packet uint crcLength = reader.ReadUInt32(); uint sessionID = reader.ReadUInt32(); uint udpBufferSize = reader.ReadUInt32(); string protocol = reader.ReadNullTerminatedString(); // Is the client using the correct protocol? if (ProtocolString == protocol) { // Can we encrypt/compress? bool encryptable = false; bool compressable = true; // Start the session and manage the client sender.StartSession(crcLength, sessionID, udpBufferSize); sender.SetCompressable(compressable); sender.SetEncryptable(encryptable); Server.ConnectionManager.AddNewClient(sender); // Setup a writer SOEWriter writer = new SOEWriter((ushort)SOEOPCodes.SESSION_RESPONSE); // Write a response writer.AddUInt32(sessionID); writer.AddUInt32(sender.GetCRCSeed()); writer.AddByte((byte)crcLength); writer.AddBoolean(compressable); writer.AddBoolean(encryptable); writer.AddUInt32(udpBufferSize); writer.AddUInt32(3); // Get the response SOEPacket response = writer.GetFinalSOEPacket(sender, false, false); // Send the response! sender.SendPacket(response); } else { // They aren't using the right protocol... Log("Got connection request from client with incorrect protocol. Client: {0}, Server: {1}", protocol, ProtocolString); } }
public void HandleDisconnect(SOEClient sender, SOEPacket packet) { // Setup a reader SOEReader reader = new SOEReader(packet); // Get the data from the packet uint sessionID = reader.ReadUInt32(); ushort reason = reader.ReadUInt16(); // Handle if (sessionID == sender.GetSessionID()) { Console.WriteLine("Disconnecting"); Server.ConnectionManager.DisconnectClient(sender, reason, true); } }
public void Receive(SOEPacket packet) { ushort opCode = packet.GetOpCode(); if (opCode == (ushort)SOEOPCodes.FRAGMENTED_RELIABLE_DATA) { ReceiveFragment(packet); } else if (opCode == (ushort)SOEOPCodes.RELIABLE_DATA) { ReceiveMessage(packet); } else if (opCode == (ushort) SOEOPCodes.ACK_RELIABLE_DATA) { // TODO: Handle repeat-until-acknowledged and all that comes with it. Log("Data Ack"); } else { // Shrug ¯\_(ツ)_/¯ Log("Received a packet that was not data or acknowledge. Discarding.."); } }
public void SendPacket(SOEPacket packet) { // Send the packet Server.SendPacket(this, packet); // This client is still alive Interact(); }
private void ReceiveMessage(SOEPacket packet) { SOEReader reader = new SOEReader(packet); // Have we received in order? ushort sequenceNumber = reader.ReadUInt16(); if ((sequenceNumber != LastReceivedSequenceNumber + 1) && (sequenceNumber != 0)) { ReceivedSequenceOutOfOrder(sequenceNumber); return; } // Acknowledge Acknowledge(sequenceNumber); LastReceivedSequenceNumber = sequenceNumber; // Get the SOEMessage byte[] data = reader.ReadToEnd(); // Handle! Client.ReceiveMessage(data); }
private void ReceiveFragment(SOEPacket packet) { // Setup a reader SOEReader reader = new SOEReader(packet); reader.ReadUInt16(); // Have we already started a fragmented packet? if (StartedFragmentedPacket) { // One less fragment till we need to acknowledge! FragmentsTillAck--; // Get our sequence number uint previousFragmentSequenceNumber = FragmentSequenceNumber; FragmentSequenceNumber = reader.ReadUInt16(); // Did we get a correct sequence number? if (FragmentSequenceNumber != previousFragmentSequenceNumber + 1) { // Out of order! ReceivedSequenceOutOfOrder(FragmentSequenceNumber); return; } // Append the rest of the packet to the fragmented data for (int i = 4; i < FragmentedData.Length; i++) { FragmentedData[ReceivedFragmentsSize] = reader.ReadByte(); ReceivedFragmentsSize++; } } else { // We're expecting the starting packet FragmentSequenceNumber = reader.ReadUInt16(); uint totalSize = reader.ReadUInt32(); // Is this a valid sequence number? if ((FragmentSequenceNumber != LastReceivedSequenceNumber + 1) && (FragmentSequenceNumber != 0)) { // Out of order! ReceivedSequenceOutOfOrder(FragmentSequenceNumber); return; } // Get the total size FragmentedData = new byte[totalSize]; // How many fragments till we need to acknowledge FragmentsTillAck = 4; // Append the rest of the packet to the fragmented data for (int i = 8; i < FragmentedData.Length; i++) { FragmentedData[ReceivedFragmentsSize] = reader.ReadByte(); ReceivedFragmentsSize++; } // Started a fragmented packet StartedFragmentedPacket = true; } // Are we finished with the fragmented data? if (ReceivedFragmentsSize >= FragmentedData.Length) { // Finish fragmented packet StartedFragmentedPacket = false; FragmentsTillAck = 0; // Handle the fragmented packet as a RELIABLE_DATA packet SOEWriter writer = new SOEWriter((ushort)SOEOPCodes.RELIABLE_DATA); writer.AddBytes(FragmentedData); SOEPacket wholePacket = writer.GetFinalSOEPacket(Client, false, false); // Receive this packet! Receive(wholePacket); return; } // Do we need to acknowledge? if (FragmentsTillAck == 0) { Acknowledge(FragmentSequenceNumber); FragmentsTillAck = 5; } }
public void SendPacket(SOEClient client, SOEPacket packet) { // Send the message UdpClient.Send(packet.GetRaw(), packet.GetLength(), client.Client); }
public void HandlePacket(SOEClient sender, SOEPacket packet) { // Operation ushort opCode = packet.GetOpCode(); // Security Measure if (!sender.HasSession()) { if (opCode != (ushort)SOEOPCodes.SESSION_REQUEST) { // We really don't care about this client. // They can try send stuff as much as possible. // TODO: Handle this (cuz attacks) return; } } // Handle! switch ((SOEOPCodes)opCode) { case SOEOPCodes.SESSION_REQUEST: HandleSessionRequest(sender, packet); break; case SOEOPCodes.MULTI: Console.WriteLine("MULTI!"); // TODO break; case SOEOPCodes.DISCONNECT: HandleDisconnect(sender, packet); break; case SOEOPCodes.PING: HandlePing(sender); break; case SOEOPCodes.RELIABLE_DATA: case SOEOPCodes.FRAGMENTED_RELIABLE_DATA: case SOEOPCodes.ACK_RELIABLE_DATA: sender.DataChannel.Receive(packet); break; default: Log("Received Unknown SOEPacket 0x{0:X2}!", packet.GetOpCode()); break; } }