public virtual void SendBytes(byte[] bytes) { var task = new SocketSendTask() { }; task.TlsStreams = tlsStreams; task.Data = bytes; task.Count = task.Data.Length; task.Completed += sendHandler; if (IsConnected) { Socket.QueueSend(task); } }
public void SendAsync(IPacket packet, IPEndPoint remoteEp) { byte[] tmp = Formatter.Format(packet); var task = new SocketSendTask(tmp) { UserToken = packet, RemoteEndPoint = remoteEp }; task.Completed += sendHandler; if (Socket != null) { Socket.QueueSend(task); } }
protected virtual void SendPacket(IPacket packet) { byte[] payload; var task = new SocketSendTask() { UserToken = packet }; var ping = packet as PingPongPacket; switch (MessageType) { case WebSocketMessageType.Text: { if (!IsWebSocket) { throw new InvalidOperationException("Can only send text packets on WebSockets."); } string id_ = $"|{packet.Id}|"; if (ping != null) { payload = ping.RawBytes; } else { payload = Formatter.FormatJson(packet, Isib0tSocket); if (payload == null) { if (IsConnected) { SendQueue(); } return; } } task.Data = EncodedPacket(ping != null ? WebSocketOpCode.Pong : WebSocketOpCode.Text, payload); break; } case WebSocketMessageType.Binary: { if (ping != null) { payload = ping.RawBytes; } else { payload = Formatter.Format(packet); } if (IsWebSocket) { payload = payload.Skip(2).ToArray(); payload = EncodedPacket(ping != null ? WebSocketOpCode.Pong : WebSocketOpCode.Binary, payload); } task.Data = payload; break; } } task.SslStream = sslStream; task.Count = task.Data.Length; task.Completed += SendComplete; if (IsConnected) { Socket.QueueSend(task); } }
public static void QueueSend(this Socket socket, SocketSendTask task) { task.Socket = socket ?? throw new ArgumentNullException("socket", "socket cannot be null"); task.Exception = null; IO.QueueWrite(task); }