private void ReceiveCallback(IAsyncResult ar) { var buff = (byte[])ar.AsyncState; try { var bytesCount = Socket.EndReceive(ar); Stats.AddBytesReceived(bytesCount); if (bytesCount > 0) { var bytesReceived = new byte[bytesCount]; Buffer.BlockCopy(buff, 0, bytesReceived, 0, bytesCount); _messagesBuffer.Put(bytesReceived); while (_messagesBuffer.Size > 4) { var messageLength = BinaryProtocol.GetMessageLength(_messagesBuffer.Peek(4)); if (_messagesBuffer.Size >= messageLength) { var messageBytes = _messagesBuffer.Get(messageLength); var proto = new BinaryProtocol(messageBytes); var cmdName = proto.GetCommandBytes(); CommandContainer <IClientCommand> command; //if (Commands.TryGetValue(cmdName, out command)) //{ // command.CommandRef.Execute(null, ); // command.CommandRef //} RaiseReceive(new ClientGenericEventArgs <byte[]> { DataReceived = bytesReceived, Protocol = proto }); } else { break; } } } Socket.BeginReceive(buff, 0, 1024, SocketFlags.None, ReceiveCallback, buff); } catch (Exception ex) { RaiseError(new ClientGenericEventArgs <Exception> { DataReceived = ex }); } }
private void EndReceive(IAsyncResult ar) { var evArgs = (SocketAsyncEventArgs)ar.AsyncState; try { var args = (SocketAsyncEventArgsExtended)evArgs; switch (args.SocketError) { case SocketError.Success: var bytesReceivedCount = args.AcceptSocket.EndReceive(ar); if (args.AcceptSocket.Connected) { var receivedBytes = new byte[bytesReceivedCount]; Buffer.BlockCopy(_receiveBuffer, 0, receivedBytes, 0, bytesReceivedCount); args.CurrentSocketStats.AddBytesReceived(args.BytesTransferred); args.LastResponse = DateTime.UtcNow; args.BytesBuffer.Put(receivedBytes); while (args.BytesBuffer.Size > 4) { var messageLength = BinaryProtocol.GetMessageLength(args.BytesBuffer.Peek(args.BytesBuffer.Size)); if (args.BytesBuffer.Size >= messageLength) { var messageBytes = args.BytesBuffer.Get(messageLength); var messageBytesCopy = new byte[BinaryProtocol.HeaderLength + 1]; Buffer.BlockCopy(messageBytes, 0, messageBytesCopy, 0, BinaryProtocol.HeaderLength + 1); var protocol = new BinaryProtocol(messageBytesCopy); var commandHash = protocol.GetCommandBytes(); Tuple <Transport.CommandsBase.IServerCommand, Type> command; var userConnection = args.UserToken as ServerConnection <byte[]>; if (Commands.TryGetValue(commandHash, out command)) { userConnection?.OnDataReceived(protocol, command.Item1, command.Item2); } ReceiveInvoke(new ServerGenericEventArgs <byte[]> { ServerConnectionBase = args.UserToken as ServerConnection <byte[]>, DataReceived = messageBytes, Protocol = protocol }); } else { break; } } } break; case SocketError.ConnectionReset: break; case SocketError.TimedOut: #if DEBUG Trace.WriteLine("SocketError.TimedOut"); #endif break; default: var connection = args.UserToken as ServerConnection <byte[]>; DisconnectClient(connection?.ConnectionId); break; } evArgs.AcceptSocket?.BeginReceive(_receiveBuffer, 0, _receiveBuffer.Length, SocketFlags.None, EndReceive, ListenArgs); } catch (Exception ex) { OnErrorInvoke(new ServerGenericEventArgs <Exception> { DataReceived = ex }); evArgs.AcceptSocket = null; } }
private void TcpOnReceive(object sender, ClientGenericEventArgs <byte[]> clientGenericEventArgs) { var proto = new BinaryProtocol(clientGenericEventArgs.DataReceived); Trace.WriteLine($"Package received {proto.GetCommandBytes()}"); }