Esempio n. 1
0
        public override void SetIoHandler(INetworkEventHandler handler)
        {
            base.SetIoHandler(handler);
            if (handler is NetworkEventHandler)
            {
                m_Events   = ((handler as NetworkEventHandler).Events) as WebSocketEventPackage;
                m_Handlers = (handler as NetworkEventHandler).GetHandlerManager();
            }

            if (m_Events != null)
            {
                m_Events.OnConnect += WhenTcpSocketConnected;
            }
        }
Esempio n. 2
0
        public Session(int id, Socket socket, INetworkEventHandler handler, INetworkFilter filter, bool needSsl = false)
        {
            m_Id = id;

            m_Stream = new NetworkStream(socket, true);
            if (needSsl)
            {
                m_Stream = new SslStream(m_Stream, false);
            }

            m_IoHandler = handler;
            m_IoFilter  = filter;

            UserData = null;
        }
Esempio n. 3
0
        public Session(int id, Socket socket, INetworkEventHandler handler, INetworkFilter filter,
                       RemoteCertificateValidationCallback validationCallback)
        {
            m_Id = id;

            m_Stream = new SslStream(new NetworkStream(socket, true), false, validationCallback);

            m_IoHandler = handler;
            m_IoFilter  = filter;

            MaxReadQueueSize  = 1024;
            MaxWriteQueueSize = 0;

            FitReadQueueAction  = ACT_KEEP_DEFAULT;
            FitWriteQueueAction = ACT_KEEP_DEFAULT;

            UserData = null;
        }
Esempio n. 4
0
        public static void StartClient <Protocol>(string remoteHost, int remotePort) where Protocol : IPeer, new()
        {
            if (CheckInited())
            {
                if (IsOnline)
                {
                    if (IsServer)
                    {
                        Debug.LogWarning("You are already a server. StartClient() is aborted.");
                    }
                    else
                    {
                        Debug.LogWarning("You are already a client. StartClient() is aborted.");
                    }
                }

                _handler = new EventHandlerClient <Protocol>(remoteHost, remotePort, () => { IsOnline = true; IsServer = false; });
            }
        }
Esempio n. 5
0
        public static void StartServer <Protocol>(int localPort = 0, int maxConnection = 1) where Protocol : IPeer, new()
        {
            if (CheckInited())
            {
                if (IsOnline)
                {
                    if (IsServer)
                    {
                        Debug.LogWarning("You are already a server. StartServer() is aborted.");
                    }
                    else
                    {
                        Debug.LogWarning("You are already a client. StartServer() is aborted.");
                    }
                }

                _handler = new EventHandlerServer <Protocol>(localPort, maxConnection);
                IsOnline = true;
            }
        }
Esempio n. 6
0
        public Session(int id, Socket socket, INetworkEventHandler handler, INetworkFilter filter, bool needSsl = false)
        {
            m_Id = id;

            m_Stream = new NetworkStream(socket, true);
            if (needSsl)
            {
                m_Stream = new SslStream(m_Stream, false);
            }

            m_IoHandler = handler;
            m_IoFilter  = filter;

            MaxReadQueueSize  = 1024;
            MaxWriteQueueSize = 0;

            FitReadQueueAction  = ACT_KEEP_DEFAULT;
            FitWriteQueueAction = ACT_KEEP_DEFAULT;

            UserData = null;
        }
Esempio n. 7
0
        void Update()
        {
            // StartServer나 StartClient가 호출되어 초기화되기 전엔 아래의 코드를 실행하지 않음
            if (_handler == null)
            {
                return;
            }

            // 핸들러가 모든 리소스를 정리하고 끝마칠 준비가 돼야 제거한다.
            // 서버나 클라이언트를 종료하고 난 후에도 처리해야할 이벤트가 있을 수 있기 때문이다.
            if (_handler.IsDead())
            {
                _handler.OnRemoved();
                _handler = null;
                IsOnline = false;
                IsServer = true;
                return;
            }

            // Notice we process all network events until we get a 'Nothing' response here.
            // Often people just process a single event per frame, and that results in very poor performance.
            var noEventsLeft = false;

            while (!noEventsLeft)
            {
                byte errorCode;
                int  dataSize;
                int  channelId;
                int  connectionId;
                int  hostId;

                var netEventType = NetworkTransport.Receive(
                    out hostId,
                    out connectionId,
                    out channelId,
                    _buffer,
                    _buffer.Length,
                    out dataSize,
                    out errorCode);

                var error = (NetworkError)errorCode;
                if (error != NetworkError.Ok)
                {
                    Debug.Log(string.Format("NetworkTransport error : {0}", error));
                    _handler.OnError(hostId, connectionId, channelId, error);
                    return;
                }

                switch (netEventType)
                {
                case NetworkEventType.Nothing:
                    noEventsLeft = true;
                    break;

                case NetworkEventType.ConnectEvent:
                    _handler.OnConnectEvent(hostId, connectionId, channelId);
                    break;

                case NetworkEventType.DataEvent:
                    _handler.OnDataEvent(hostId, connectionId, channelId, _buffer, dataSize);
                    break;

                case NetworkEventType.DisconnectEvent:
                    _handler.OnDisconnectEvent(hostId, connectionId, channelId);
                    break;
                }
            }
        }
Esempio n. 8
0
 public virtual void SetIoHandler(INetworkEventHandler handler)
 {
     m_IoHandler = handler;
 }
Esempio n. 9
0
        public Session(int id, Socket socket, INetworkEventHandler handler, INetworkFilter filter, bool needSsl = false)
        {
            m_Id = id;

            m_Stream = new NetworkStream(socket, true);
            if (needSsl) m_Stream = new SslStream(m_Stream, false);

            m_IoHandler = handler;
            m_IoFilter = filter;

            UserData = null;
        }
Esempio n. 10
0
        public Session(int id, Socket socket, INetworkEventHandler handler, INetworkFilter filter, 
                        RemoteCertificateValidationCallback validationCallback)
        {
            m_Id = id;

            m_Stream = new SslStream(new NetworkStream(socket, true), false, validationCallback);

            m_IoHandler = handler;
            m_IoFilter = filter;

            UserData = null;
        }
Esempio n. 11
0
 public virtual void SetIoHandler(INetworkEventHandler handler)
 {
     m_IoHandler = handler;
 }
Esempio n. 12
0
 public MessageClient(INetworkFilter filter, INetworkEventHandler handler)
     : this()
 {
     SetIoFilter(filter);
     SetIoHandler(handler);
 }
Esempio n. 13
0
 public void AddHost(int id, INetworkEventHandler eventHandler)
 {
     _hosts[id] = eventHandler;
 }