Example #1
0
        public void Open()
        {
            if (m_State >= 0)
            {
                return;               // do nothing if the session is already in "connected" or "connecting" state
            }
            if (m_SessionGroup != null)
            {
                // first, add it to group ...
                m_SessionGroup.AddSession(m_Id, this);
            }

            m_LastReadTime  = DateTime.Now;
            m_LastWriteTime = DateTime.Now;

            Socket socket = GetSocket();

            if (socket != null)
            {
                try
                {
                    m_State      = 1;
                    m_RemoteIp   = IPAddress.Parse(((IPEndPoint)socket.RemoteEndPoint).Address.ToString()).ToString();
                    m_RemotePort = ((IPEndPoint)socket.RemoteEndPoint).Port;
                }
                catch { }
            }

            try
            {
                // make sure the queue is clean
                if (m_OutgoingMessageQueue.Count > 0)
                {
                    lock (m_OutgoingMessageQueue) m_OutgoingMessageQueue.Clear();
                }
                // make sure the queue is clean
                if (m_IncomingMessageQueue.Count > 0)
                {
                    m_IncomingMessageQueue.Clear(); // incoming data should be processed in single thread
                }
            }
            catch { }

            if (m_IoHandler != null)
            {
                try
                {
                    m_IoHandler.OnConnect(this);
                }
                catch { }
            }

            try
            {
                if (socket != null)
                {
                    if (m_ReadBuffer == null || m_ReadBuffer.Length <= 0)
                    {
                        m_ReadBuffer = new Byte[socket.ReceiveBufferSize];
                    }
                }

                if (m_Stream != null)
                {
                    m_Stream.BeginRead(m_ReadBuffer, 0, m_ReadBuffer.Length, new AsyncCallback(ReceiveCallback), this);
                }
            }
            catch (Exception ex)
            {
                if (m_IoHandler != null)
                {
                    try
                    {
                        m_IoHandler.OnError(this, ERROR_RECEIVE, ex);
                    }
                    catch { }
                }
            }
        }
Example #2
0
        public void ReceiveIncomingMessages()
        {
            byte             networkError;
            int              connectionId;
            int              channelId;
            int              receivedSize;
            NetworkEventType networkEvent;

            if (HostId >= 0 && NetworkEventHandler != null)
            {
                do
                {
                    networkEvent = NetworkTransport.ReceiveFromHost(HostId, out connectionId, out channelId, NetBuffer.InternalBuffer, NetBuffer.InternalBuffer.Length, out receivedSize, out networkError);
                    switch (networkEvent)
                    {
                    case NetworkEventType.ConnectEvent:
                        if ((NetworkError)networkError == NetworkError.Ok)
                        {
                            if (Mode == OnlineSessionMode.Server)
                            {
                                ClientConnections.Add(new ClientConnection(connectionId));
                            }
                            else if (Mode == OnlineSessionMode.Client)
                            {
                                ServerConnection = connectionId;
                            }
                            NetworkEventHandler.OnConnect(connectionId);
                        }
                        break;

                    case NetworkEventType.DisconnectEvent:
                        if (Mode == OnlineSessionMode.Server)
                        {
                            for (int i = ClientConnections.Count - 1; i >= 0; i--)
                            {
                                if (ClientConnections[i].ConnectionId == connectionId)
                                {
                                    ClientConnections.RemoveAt(i);
                                    break;
                                }
                            }
                        }
                        else if (Mode == OnlineSessionMode.Client)
                        {
                            ServerConnection = -1;
                        }

                        NetworkEventHandler.OnDisconnect(connectionId);

                        if (Mode == OnlineSessionMode.Client)
                        {
                            TerminateNetworking();
                        }
                        break;

                    case NetworkEventType.DataEvent:
                        if ((NetworkError)networkError == NetworkError.Ok)
                        {
                            NetworkEventHandler.OnData(connectionId, channelId, receivedSize);
                        }
                        break;
                    }
                }while (networkEvent != NetworkEventType.Nothing);
            }
        }