public virtual async Task ConnectToServerAsync(IPAddress ipAddress, int port) { if (ipAddress == null) throw new ArgumentNullException("ipAddress"); if (port <= 0) throw new ArgumentOutOfRangeException("Port must be greater than zero"); CheckIfDisposed(); // Connect to the server await socket.ConnectAsync(ipAddress, port); // Receive the initial welcome message packet var welcomePacket = await ReceiveNextPacket(); Debug.WriteLine(string.Format("ReceivedPacket: {0}", welcomePacket)); // Get the welcome message ASCII bytes, skipping the first ESC control character (0x1B) var welcomeString = Encoding.ASCII.GetString(welcomePacket.Data.Skip(1).ToArray()); // Check that the welcome message equals the expected string value if (!string.Equals(welcomeString, ServerWelcomeMessage, StringComparison.Ordinal)) throw new Exception("A service was running at the specified location, but it doesn't appear to be a Darkages server."); // Create a response packet var responsePacket = new NetworkPacket(0xAA, 0x00, 0x0A, 0x62, 0x00, 0x34, 0x00, 0x0A, 0x88, 0x6E, 0x59, 0x59, 0x75); var responsePacketData = responsePacket.ToArray(); // Send the response data to the server await socket.SendAsync(responsePacketData, 0, responsePacketData.Length); Debug.WriteLine(string.Format("SentPacket: {0}", responsePacket)); }
public virtual async Task<bool> CheckClientVersionCodeAsync(int versionCode) { if (versionCode <= 0) throw new ArgumentOutOfRangeException("Version code must be greater than zero"); CheckIfDisposed(); // Create a client version packet var version = (short)versionCode; var versionPacket = new NetworkPacket(0xAA, 0x00, 0x06, 0x00, version.HiByte(), version.LoByte(), 0x4C, 0x4B, 0x00); var versionPacketData = versionPacket.ToArray(); // Send the version packet to the server await socket.SendAsync(versionPacketData, 0, versionPacketData.Length); Debug.WriteLine(string.Format("SentPacket: {0}", versionPacket)); // Receive the response from the server var responsePacket = await ReceiveNextPacket(); Debug.WriteLine(string.Format("ReceivedPacket: {0}", responsePacket)); // Get the status code from the response var statusCode = responsePacket.Data[0]; if (statusCode == 0x1) // SERVER MESSAGE { // Get the server message and raise an exception var serverMessage = Encoding.ASCII.GetString(responsePacket.Data.Skip(1).ToArray()); throw new Exception(string.Format("The server has rejected client version {0}.", versionCode)); } else if (statusCode == 0x2) // PATCH REQUEST { // Get the required version and raise an exception var requiredVersion = IntegerExtender.MakeWord(responsePacket.Data[2], responsePacket.Data[1]); throw new Exception(string.Format("The server requires client version {0}.", requiredVersion)); } // Version was accepted by the server return true; }
void ProcessPacketBuffer() { // Process buffer while there is at least one possible packet while (buffer.Count > NetworkPacket.HeaderSize) { // Get the expected size of the packet var size = IntegerExtender.MakeWord(buffer[2], buffer[1]); var packetSize = size + 3; // If the entire packet has not been buffered, stop processing until more data arrives if (packetSize > buffer.Count) break; // Copy packet from buffer var dataOffset = 4; var dataSize = packetSize - dataOffset; var packet = new NetworkPacket(buffer[0], size, buffer[3], buffer.GetRange(dataOffset, dataSize)); // Queue packet and remove bytes from buffer packets.Enqueue(packet); buffer.RemoveRange(0, packetSize); } }