public WebSocketSessionBase(INetApplication application, INetProtocol protocol, INetServer server, Socket clientSocket, ILogger logger)
     : base(application, protocol, server, clientSocket)
 {
     IsHandShake    = false;
     ReceiveTimeout = TimeSpan.FromMinutes(2);
     this.logger    = logger;
 }
Exemple #2
0
        public void ComposeOutput(INetProtocol netProtocol)
        {
            OutStream outStream = new OutStream();

            netProtocol.OnSerialize(outStream);
            NetworkManager.Instance.OnWrite(outStream);
        }
Exemple #3
0
        /// <summary>
        /// This constructor should be used if a new connection is being created, for example in a client side application.
        /// The <code>TcpProtocolListener</code> should not attempt to use this constructor.
        /// </summary>
        /// <param name="protocol_"></param>
        /// <param name="hostnameOrIpAddress_"></param>
        /// <param name="port_"></param>
        /// <param name="resolver_"></param>
        public TcpProtocolClient(INetProtocol protocol_, string hostnameOrIpAddress_, int port_, IIpEndPointResolver resolver_ = null)
        {
            if (null == protocol_)
            {
                throw new ArgumentNullException(nameof(protocol_));
            }

            if (null == hostnameOrIpAddress_)
            {
                throw new ArgumentNullException(nameof(hostnameOrIpAddress_));
            }

            if (string.IsNullOrWhiteSpace(hostnameOrIpAddress_))
            {
                throw new ArgumentException("Empty hostname provided");
            }

            Protocol         = protocol_;
            Hostname         = hostnameOrIpAddress_;
            Port             = port_;
            EndPointResolver = resolver_ ?? new DnsEndPointResolver();

            // Don't create the tcp client yet... we need the IPAddressFamily so create it at the point Connect is called
            // to allow the caller to have a chance to change the EndPointResolver.
        }
        public virtual bool Setup(INetServerConfig config, INetApplication application, INetProtocol protocol, ISocketSessionFactory sessionFactory)
        {
            if (protocol == null)
            {
                throw new ArgumentNullException("protocol");
            }
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }
            if (application == null)
            {
                throw new ArgumentNullException("application");
            }

            this.protocol       = protocol;
            this.application    = application;
            this.config         = config;
            this.endPoint       = new IPEndPoint(config.Address, config.Port);
            this.sessionFactory = sessionFactory;

            this.status    = NetServerStatus.Inited;
            this.IsRunning = false;

            return(true);
        }
Exemple #5
0
        public WebSocketClientSession(WebSocketClient client, INetProtocol protocol, Socket clientSocket)
            : base(null, protocol, null, clientSocket, LoggerManager.GetLogger("WebSocketClientSession"))
        {
            FrameReader = new FrameStreamReader(frameStream);

            Client = client;
        }
Exemple #6
0
 public AsyncTcpSession(INetApplication application, INetProtocol protocol, INetServer server, Socket clientSocket)
     : base(application, protocol, server, clientSocket)
 {
     asyncEventArgs = new SocketAsyncEventArgs();
     Byte[] buffer = new byte[255];
     asyncEventArgs.SetBuffer(buffer, 0, 255);
     asyncEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(AsyncEventArgs_Completed);
 }
Exemple #7
0
        protected void HandleReceived(byte[] data, int len)
        {
            // Identify the protocol user is connecting with
            // It could be ClassiCube, Minecraft Modern, WebSocket, etc
            if (protocol == null)
            {
                protocol = IdentifyProtocol(data[0]);
                if (protocol == null)
                {
                    return;
                }
            }
            byte[] src;

            // Is there any data leftover from last Receive call?
            if (leftLen == 0)
            {
                src     = data;
                leftLen = len;
            }
            else
            {
                // Yes, combine it with new data
                int totalLen = leftLen + len;
                if (totalLen > leftData.Length)
                {
                    Array.Resize(ref leftData, totalLen);
                }

                Buffer.BlockCopy(data, 0, leftData, leftLen, len);
                src     = leftData;
                leftLen = totalLen;
            }

            // Packets may not always be fully received
            // Hence may need to retain partial packet data after processing
            int processedLen = protocol.ProcessReceived(src, leftLen);

            leftLen -= processedLen;
            if (leftLen == 0)
            {
                return;
            }

            if (leftData == null || leftLen > leftData.Length)
            {
                leftData = new byte[leftLen];
            }
            // move remaining partial packet data back to start of leftover/remaining buffer
            for (int i = 0; i < leftLen; i++)
            {
                leftData[i] = src[processedLen + i];
            }
        }
Exemple #8
0
        public void ResolveInput(InStream inStream)
        {
            ushort protocolID;

            inStream.ReadUnsignedShort(out protocolID);

            INetProtocol netProtocol = GenerateProtocol(protocolID, this);

            netProtocol.Deserialize(inStream);
            netProtocol.Excute();
        }
Exemple #9
0
        void IdentifyProtocol(byte opcode)
        {
            ProtocolConstructor cons = Protocols[opcode];

            if (cons != null)
            {
                protocol = cons(this);
            }
            if (protocol != null)
            {
                return;
            }

            Logger.Log(LogType.UserActivity, "Disconnected {0} (unknown opcode {1})", IP, opcode);
            Close();
        }
        public INetSession CreateSession(INetApplication application, INetProtocol protocol, INetServer server, System.Net.Sockets.Socket client)
        {
            SocketSession      s      = new WebSocketSession(application, protocol, server, client);
            SocketServerConfig config = null;

            if (server is SocketServerBase)
            {
                config = (server as SocketServerBase).Config;
            }
            if (config != null)
            {
                s.Timeout     = TimeSpan.FromMinutes(config.SessionTimeout);
                s.TimeoutType = config.TimeoutType;
            }

            return(s);
        }
        protected virtual void Init(INetApplication application, INetProtocol protocol, INetServer server, Socket clientSocket)
        {
            this.application      = application;
            this.protocol         = protocol;
            this.server           = server;
            this.clientSocket     = clientSocket;
            this.sessionID        = Guid.NewGuid().ToString("N").ToLower();
            this.startTime        = DateTime.Now;
            this.ActiveTime       = DateTime.Now;
            this.Timeout          = TimeSpan.FromMinutes(10);
            this.LastRequestTime  = DateTime.Now;
            this.LastResponseTime = DateTime.Now;
            this.TimeoutType      = SessionTimeoutType.Active;

            if (clientSocket != null)
            {
                InnerStream = new NetworkStream(this.clientSocket);
            }
        }
Exemple #12
0
        /// <summary>
        /// This constructor should only be used when an established TCP connection is present. The provided TCP client should
        /// already be in a connected state.
        /// </summary>
        /// <param name="protocol_"></param>
        /// <param name="client_"></param>
        public TcpProtocolClient(INetProtocol protocol_, TcpClient client_)
        {
            if (null == protocol_)
            {
                throw new ArgumentNullException(nameof(protocol_));
            }

            if (null == client_)
            {
                throw new ArgumentNullException(nameof(client_));
            }

            try
            {
                if (!client_.Connected)
                {
                    throw new ArgumentException("Non connected TcpClient provided");
                }
            }
            catch (Exception exp)
            {
                throw new ArgumentException("Non connected TcpClient provided", exp);
            }

            Protocol        = protocol_;
            Client          = client_;
            Protocol.Stream = Client.GetStream(); // Need to do this upfront too to ensure IsConnected works

            // Bit odd to throw parameter exceptions after accepting and assigning them...
            // just want to double check though that everything worked and we are connected as per this objects
            // definition of being connected.
            if (!IsConnected)
            {
                throw new ArgumentException("The provided TcpClient object is not connected.");
            }
        }
Exemple #13
0
        // private static ILogger logger = null;


        public WebSocketSession(INetApplication application, INetProtocol protocol, INetServer server, Socket clientSocket)
            : base(application, protocol, server, clientSocket, LoggerManager.GetLogger("WebSocketSession"))
        {
        }
Exemple #14
0
 public UdpSession(INetApplication application, INetProtocol protocol, INetServer server, Socket clientSocket)
     : base(application, protocol, server, clientSocket)
 {
 }
Exemple #15
0
        public override bool Setup(INetServerConfig config, INetApplication application, INetProtocol protocol, ISocketSessionFactory sessionFactory)
        {
            if (base.Setup(config, application, protocol, sessionFactory))
            {
                Logger = LoggerManager.GetLogger(String.Concat(Application.Name, ".AsyncTcp.", config.Port));
                this.sessionFactory = sessionFactory;
                return(true);
            }

            return(false);
        }
 public SocketSession(INetApplication application, INetProtocol protocol, INetServer server, Socket clientSocket)
 {
     Init(application, protocol, server, clientSocket);
 }
 public virtual bool Setup(INetServerConfig config, INetApplication application, INetProtocol protocol)
 {
     return(false);
 }