Esempio n. 1
0
        /// <summary>
        /// Processes a received packet.
        /// </summary>
        /// <param name="packet">The packet.</param>
        void ProcessPacket(byte[] packet, Socket client, ClientSocketState userToken)
        {
            var packetObj = PacketCodec.DecodePacket(packet);
            var type      = Type.GetType(Encoding.UTF8.GetString(packetObj.Words[0]));

            if (messageHandlers.ContainsKey(type))
            {
                messageHandlers[type].Process(new ProcessingClient(packetObj, client, this));
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Processes a received packet.
        /// </summary>
        /// <param name="packet">The packet.</param>
        void ProcessPacket(byte[] packet, Socket client, ClientSocketState userToken)
        {
            var packetObj = PacketCodec.DecodePacket(packet);

#if !SILVERLIGHT
            var type = Type.GetType(Encoding.UTF8.GetString(packetObj.Words[0]));
#else
            var bytes = packetObj.Words[0];
            var type  = Type.GetType(Encoding.UTF8.GetString(bytes, 0, bytes.Length));
#endif

            if (messageHandlers.ContainsKey(type))
            {
                messageHandlers[type].Process(new ProcessingClient(packetObj, client, this));
            }
        }
Esempio n. 3
0
        void ProcessAccept(Socket serverSock, SocketAsyncEventArgs e)
        {
            var newSocket     = e.AcceptSocket;
            var readEventArgs = new SocketAsyncEventArgs();
            var state         = new ClientSocketState();

            readEventArgs.UserToken = state;
            readEventArgs.SetBuffer(state.Header, 0, state.Header.Length);
            readEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(readEventArgs_Completed);

            if (!newSocket.ReceiveAsync(readEventArgs))
            {
                ProcessReceive(newSocket, readEventArgs);
            }

            e.AcceptSocket = null;
            serverSock.AcceptAsync(e);
        }
Esempio n. 4
0
        /// <summary>
        /// Processes a received packet.
        /// </summary>
        /// <param name="packet">The packet.</param>
        void ProcessPacket(byte[] packet, ClientSocketState userToken)
        {
            var packetObj = PacketCodec.DecodePacket(packet);

            if (_registeredCallbacks.ContainsKey(packetObj.Sequence))
            {
                var callback = _registeredCallbacks[packetObj.Sequence];
                _registeredCallbacks.Remove(packetObj.Sequence);
                if (packetObj.Words.Length < 3)
                {
                    callback(this, null);
                }
                else
                {
                    callback(this, packetObj.Words[2]);
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Attempts to connect to the game server for rcon operations.
        /// </summary>
        /// <returns>True if connection established, false otherwise.</returns>
        public virtual bool Connect()
        {
            if (Connected)
            {
                Disconnect();
            }

            Connected   = false;
            _sequenceID = 1;

            try
            {
                _sock = new Socket(Endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                _sock.Connect(Endpoint);

                var readEventArgs = new SocketAsyncEventArgs();
                var state         = new ClientSocketState();
                readEventArgs.UserToken = state;
                readEventArgs.SetBuffer(state.Header, 0, state.Header.Length);
                readEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(readEventArgs_Completed);

                if (!_sock.ReceiveAsync(readEventArgs))
                {
                    ProcessReceive(_sock, readEventArgs);
                }

                Connected = true;
                return(true);
            }
            catch (Exception ex)
            {
                LastException = ex;
            }
            Disconnect();
            return(false);
        }