private static void ReceiveMessageBase(ReceiveMessageState data) { var buffer = new byte[4]; data.Buffer = buffer; NetworkIo.Receive(data.Socket, data.Buffer, 0, 4, data, EndReceiveLengthCallback); }
private static void HandshakeMessageBase(ReceiveMessageState data) { var buffer = new byte[HandshakeMessage.Length]; data.Buffer = buffer; NetworkIo.Receive(data.Socket, data.Buffer, 0, buffer.Length, data, EndReceiveHandshakeCallback); }
protected virtual void ConnectPeer(IPEndPoint ep) { if (Stopping) { return; } var peerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); NetworkIo.Connect(peerSocket, ep, peerSocket, PeerConnected); }
private static void EndReceiveLength(bool success, int read, object state) { var data = (ReceiveMessageState)state; try { if (success) { var messageLength = IPAddress.HostToNetworkOrder(BitConverter.ToInt32(data.Buffer, 0)); if (messageLength > MaxMessageLength) { data.Callback(false, null, data.State); ReceiveCache.Put(data); return; } if (messageLength == 0) { data.Callback(true, new KeepAliveMessage(), data.State); ReceiveCache.Put(data); return; } var newBuffer = new byte[read + messageLength]; Buffer.BlockCopy(newBuffer, 0, data.Buffer, 0, read); data.Buffer = newBuffer; NetworkIo.Receive(data.Socket, data.Buffer, read, messageLength, data, EndReceiveCallback); } else { data.Callback(false, null, data.State); ReceiveCache.Put(data); } } catch (Exception e) { data.Callback(false, null, data.State); ReceiveCache.Put(data); Debug.WriteLine(e, "MessageIO.EndReceiveLength"); } }
private static void SendMessageBase(SendMessageState data) { NetworkIo.Send(data.Socket, data.Buffer, data.Offset, data.Count, data, EndSendCallback); }