private void SaveMsg(NetworkMessage msg, ConnectionDestination destination) { if (Client.Silent) { return; } try { if (!Directory.Exists(Path.Combine(System.Windows.Forms.Application.StartupPath, @"Logs\"))) { Directory.CreateDirectory(Path.Combine(System.Windows.Forms.Application.StartupPath, @"Logs\")); } using (System.IO.StreamWriter file = new System.IO.StreamWriter(Path.Combine(System.Windows.Forms.Application.StartupPath, @"Logs\Network.txt"), true)) { if (destination == ConnectionDestination.Server) { file.WriteLine(string.Format("({0}) Received <- Name={1}; Id={2};", DateTime.Now.ToString("HH:mm:ss"), msg.ToString(), msg.MessageId)); } else if (destination == ConnectionDestination.Client) { file.WriteLine(string.Format("({0}) Sent -> Name={1}; Id={2};", DateTime.Now.ToString("HH:mm:ss"), msg.ToString(), msg.MessageId)); } file.Close(); file.Dispose(); } } catch (Exception) {} }
public MessageEntry(NetworkMessage msg, ConnectionDestination origin) { Message = msg; Origin = origin; Id = msg.MessageId; Name = msg.ToString(); }
public void Send(NetworkMessage message, ConnectionDestination destination = ConnectionDestination.Server) { if (message is BasicStatMessage || message.Cancel) { return; } try { if (Client.Network == null) { return; } Instance += 1; BigEndianWriter writer = new BigEndianWriter(); switch (destination) { case ConnectionDestination.Client: message.Pack(writer, 0); Client.Network.ClientConnection.Send(writer.Data); SaveMsg(message, ConnectionDestination.Client); if (ServerMessageReceived != null) { OnServerMessageReceived(new MessageReceivedEventArgs(message)); } break; case ConnectionDestination.Server: message.Pack(writer, Instance); Client.Network.ServerConnection.Send(writer.Data); SaveMsg(message, ConnectionDestination.Server); if (ClientMessageReceived != null) { OnClientMessageReceived(new MessageReceivedEventArgs(message)); } break; } writer.Dispose(); writer = null; message = null; } catch (Exception ex) { if (Client != null && WindowManager.MainWindow.Logger != null) { WindowManager.MainWindow.Logger.Error("[Network] Send Function -> " + ex.Message); } else { WindowManager.MainWindow.Logger.Error("[Network] Send Function -> " + ex.Message); } } }
public void Send(MessagePart part, ConnectionDestination destination) { try { BigEndianWriter writer = new BigEndianWriter(); byte typeLen; if (part.Data.Length > 65535) { typeLen = 3; } else if (part.Data.Length > 255) { typeLen = 2; } else if (part.Data.Length > 0) { typeLen = 1; } else { typeLen = 0; } writer.WriteShort((short)(part.MessageId << 2 | typeLen)); Instance += 1; if (destination == ConnectionDestination.Server) { writer.WriteUInt(Instance); } switch (typeLen) { case 0: break; case 1: writer.WriteByte((byte)part.Data.Length); break; case 2: writer.WriteShort((short)part.Data.Length); break; case 3: writer.WriteByte((byte)(part.Data.Length >> 16 & 255)); writer.WriteShort((short)(part.Data.Length & 65535)); break; } writer.WriteBytes(part.Data); if (writer == null || writer.Data == null) { return; } switch (destination) { case ConnectionDestination.Client: ClientConnection.Send(writer.Data); break; case ConnectionDestination.Server: ServerConnection.Send(writer.Data); break; } writer.Dispose(); writer = null; part = null; } catch (Exception ex) { if (Client != null && WindowManager.MainWindow.Logger != null) { WindowManager.MainWindow.Logger.Error("[Network] Send Function -> " + ex.Message); } else { WindowManager.MainWindow.Logger.Error("[Network] Send Function -> " + ex.Message); } } }