protected void BeginPacket(PacketType type) { if (type != PacketType.Ping && type != PacketType.Pong) { LogInterface.Log(string.Format("BeginPacket({0})", type), LogInterface.LogMessageType.Debug); } BeginPacket((ushort)type); }
protected void SendPacket() { if (_outgoingPacket == null) { throw new InvalidOperationException("Can not send packet that has not been started!"); } byte[] data = _outgoingPacket.ToArray(); _socket.Send(data); _lastSent = DateTime.Now; LogInterface.Log(string.Format("SendPacket {0} bytes", data.Length), LogInterface.LogMessageType.Debug); _outgoingBW.Dispose(); _outgoingBW = null; _outgoingPacket = null; }
void ProcessPacketData(byte[] data, int dataLength) { // Find the packet marker int packetStart = 0; while (packetStart < data.Length - 4) { if (data[packetStart] == '$' && data[packetStart + 1] == 'R' && data[packetStart + 2] == 'G' && data[packetStart + 3] == '$') { break; } } if (packetStart > 0) { LogInterface.Log(string.Format("Connection threw away {0} bytes before packet marker", packetStart), LogInterface.LogMessageType.Debug); } MemoryStream ms = new MemoryStream(data); ms.Seek(packetStart, SeekOrigin.Begin); BinaryReader br = new BinaryReader(ms); int marker = br.ReadInt32(); ushort packetType = br.ReadUInt16(); LogInterface.Log(string.Format("Processing packet type {0}", packetType), LogInterface.LogMessageType.Debug); if (_packetHandlers.ContainsKey(packetType)) { _packetHandlers[packetType](br); int bytesProcessed = (int)ms.Position; if (bytesProcessed < dataLength) { int remaining = dataLength - bytesProcessed; byte[] remainingData = new byte[remaining]; Buffer.BlockCopy(data, bytesProcessed, remainingData, 0, remaining); _pendingData = new List <byte>(); _pendingData.AddRange(remainingData); } } else { LogInterface.Log(string.Format("Unhandled packet type {0}", packetType), LogInterface.LogMessageType.Error); } }
public virtual void Connect(string address, int port) { if (_socket != null) { throw new InvalidOperationException("Cant connect when a socket already exists"); } try { _socket = new Socket(); _socket.Connect(address, port); } catch (Exception ex) { LogInterface.Log(string.Format("Failed to connect to {0}:{1}\n{2}", address, port, ex.ToString()), LogInterface.LogMessageType.Error, true); _socket = null; } }
public LogInterface() { _log = this; }
void BeginPacket(GCPacketType type) { LogInterface.Log(string.Format("BeginPacket({0})", type), LogInterface.LogMessageType.Debug); BeginPacket((ushort)type); }