public void Native()
 {
     var serializer     = new BinaryFormaterGenericSerializer <LogInRequest>();
     var bytes          = BinaryProtocol.FromData(_loginCmd, "LogInRequest", serializer);
     var receivedPacket = new BinaryProtocol(bytes.GetDataToSend());
     var obj            = serializer.Deserialize(receivedPacket.Data);
 }
Exemplo n.º 2
0
 public void GroBuf()
 {
     var serializer     = new GrobufBinarySerializer <LogIn>();
     var bytes          = BinaryProtocol.FromObject(_loginCmd, "LogIn", serializer);
     var receivedPacket = new BinaryProtocol(bytes.GetDataToSend());
     var obj            = serializer.Deserialize(receivedPacket.Data);
 }
Exemplo n.º 3
0
        private static void TcpServerOnReceive(object sender, ServerGenericEventArgs <byte[]> serverGenericEventArgs)
        {
            var bp  = new BinaryProtocol(serverGenericEventArgs.DataReceived);
            var msg = Encoding.UTF8.GetString(bp.GetBody());

            Console.WriteLine($"{msg} received");
        }
Exemplo n.º 4
0
        private static void ClientOnOnConnected(object sender, EventArgs eventArgs)
        {
            var str   = Encoding.UTF8.GetBytes("Hello server");
            var proto = new BinaryProtocol(str, "Nop");

            _client.SendData(proto);
            Done.WaitOne(1000);
            Done.Set();
        }
        public void RaisePacketReceived(ServerConnectionBase serverConnection)
        {
            var l = new LogInRequest {
                Email = "[email protected]", Password = "******"
            };
            var bp = BinaryProtocol.FromData(l, new ProtoBufGenericSerializer <LogInRequest>());

            _serverTransport.RaiseReceive(typeof(LogInRequest), bp.GetDataToSend(), serverConnection);
        }
Exemplo n.º 6
0
        public void RaiseReceive(Type comd, byte[] bytes, ServerConnectionBase serverConnection)
        {
            var conn  = serverConnection as ServerConnection <byte[]>;
            var proto = new BinaryProtocol(bytes);

            conn?.OnDataReceived(proto, new LogInRequest(), comd);
            OnReceive?.Invoke(this, new ServerGenericEventArgs <byte[]> {
                ServerConnectionBase = serverConnection, Protocol = new BinaryProtocol(bytes), DataReceived = bytes
            });
        }
Exemplo n.º 7
0
        public void Execute <T>(ServerConnection <T> connection, IServerCommand entity)
        {
            var loginValue = entity as LogInRequest;

            var response = new LogInResponse {
                IsOk = true
            };
            var protocol = BinaryProtocol.FromData(response, new ProtoBufGenericSerializer <LogInResponse>());

            connection.SendData(protocol);
        }
        public void CreatePacket()
        {
            const string str              = "not a very big deal";
            var          bytes            = new [] { byte.MinValue, byte.MaxValue, byte.MaxValue };
            var          protocol         = new BinaryProtocol(bytes, str);
            var          protocolBytes    = protocol.GetDataToSend();
            var          protocolRestored = new BinaryProtocol(protocolBytes);
            var          size             = BinaryProtocol.GetMessageLength(protocolBytes);

            Assert.True(protocolRestored.IsCheckSumValid());
            Assert.True(protocolBytes.Length == size);
        }
Exemplo n.º 9
0
        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
                });
            }
        }
 public static ProtocolBase <byte[]> GetProtocol(T data)
 {
     return(BinaryProtocol.FromData(data, GetSerializer()));
 }
        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;
            }
        }
Exemplo n.º 12
0
        private void TcpOnReceive(object sender, ClientGenericEventArgs <byte[]> clientGenericEventArgs)
        {
            var proto = new BinaryProtocol(clientGenericEventArgs.DataReceived);

            Trace.WriteLine($"Package received {proto.GetCommandBytes()}");
        }