Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void dataArrival(IAsyncResult iAr)
- {
- if (Session == null || !Session.isValid) // Safety above all
- {
- return;
- }
- try
- {
- int bytesReceived = Socket.EndReceive(iAr);
- receivedBytesCounter += bytesReceived;
- string Data = System.Text.Encoding.Default.GetString(dataBuffer, 0, bytesReceived);
- if (Data.Length <= 0)
- {
- return;
- }
- Logging.Log("Session " + Session.ID + "<< " + Data, Logging.logType.debugEvent);
- if (encryptionClient != null)
- {
- Data = encryptionClient.Decipher(Data);
- }
- if (Data == "<policy-file-request/>" + Convert.ToChar(0))
- {
- Logging.Log("Session " + Session.ID + " requested the policy file.");
- Logging.Log("Sending XML cross domain policy file to session " + Session.ID + ".");
- sendMessage(xmlPolicy.getPolicy());
- Engine.Sessions.destroySession(Session.ID);
- /*string Key = Engine.Security.Ciphering.generateKey();
- serverMessage Message = new serverMessage(1); // "@A"
- Message.Append(Key);
- sendMessage(Message);
- Session.gameConnection.initializeEncryption(Key);
- Logging.Log("Initialized RC4 instance for session " + Session.ID + ", public key: " + Key);*/
- }
- else
- {
- while (Data.Length > 0)
- {
- int v = base64Encoding.Decode(Data.Substring(1, 2)); // Decode length of this message
- Request = new clientMessage(Data.Substring(3, v)); // Get message content and create clientMessage object
- //Logging.Log("Session " + Session.ID + "<< " + Request.ID + " : " + Request.encodedID + " " + Request.Content, Logging.logType.clientMessageEvent);
- processMessage();
- Data = Data.Substring(v + 3);
- }
- }
- if (Session != null && Session.isValid) // Session is still allowed to process messages
- {
- Socket.BeginReceive(dataBuffer, 0, dataBuffer.Length, SocketFlags.None, dataArrival, null);
- }
- }
- catch (ObjectDisposedException) { }
- catch (SocketException ex)
- {
- if (Session != null)
- {
- Logging.Log("USER D/C: " + ex.Message);
- Engine.Sessions.destroySession(Session.ID);
- }
- return;
- }
- catch (ArgumentOutOfRangeException ex) // Wrong structured packet
- {
- if (Session != null)
- {
- Logging.Log("USER D/C: " + ex.Message);
- Engine.Sessions.destroySession(Session.ID); // Something is wrong with client, weird data?
- }
- return;
- }
- catch (Exception ex)
- {
- Logging.Log("Unhandled exception occurred in dataArrival method of session, stack trace: \r\n" + ex.StackTrace, Logging.logType.commonError);
- }
- }
- private void processMessage()
- {
- if (Request.Content.Contains("\x01"))
- {
- Session.isValid = false;
- Engine.Sessions.destroySession(Session.ID);
- Engine.Net.Game.blackListIpAddress(ipAddress);
- Logging.Log("Session " + Session.ID + " (ip: " + ipAddress + ") sent message with char1 (message breaker), hax! IP address blacklisted.");
- return;
- }
- try
- {
- messageHandler.handleRequest(Request);
- }
- catch (Exception e)
- {
- Logging.Log("Unhandled error occurred in processMessage(). Error: " + e.Message);
- // Transmit client error report
- Response.Initialize(299); // "Dk"
- Response.appendWired(1337); // Server ID
- Response.appendWired(Request.ID); // Last message ID
- Response.appendClosedValue(DateTime.Now.ToString());
- sendResponse();
- return;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement