Exemplo n.º 1
0
        // TODO: Data Receive, Packet Assembly and Dispatch
        // should drive by unity update event
        public void BufferProcess()
        {
            int  processCnt = 0;
            int  recvHostId;
            int  recvConnId;
            int  recvChannelId;
            int  bufferSize = NetworkConst.MAX_PACKET_LEN;
            int  dataSize;
            byte error;


            // PROCESS ONLY ONE PACKET PER FRAME IS NOT A GOOD IDEA, That's why here is a while loop
            NetworkEventType evt;

            do
            {
                m_Buffer.Reset();
                evt = NetworkTransport.Receive(out recvHostId, out recvConnId,
                                               out recvChannelId, m_Buffer.BufferArray, bufferSize, out dataSize, out error);
                switch (evt)
                {
                case NetworkEventType.ConnectEvent:
#if UNITY_EDITOR
                    Debug.Log("<color=blue>Connection Established</color>");
#endif
                    RemoteTerminalInfo info = RegisterRemoteTerminal(recvConnId, recvHostId);
                    EvtDispatcher.Instance.Dispatch(NetworkEvt.EVT_ON_CONNECTION_ESTABLISH, info);
                    break;

                case NetworkEventType.DisconnectEvent:
#if UNITY_EDITOR
                    Debug.Log("<color=red>Connection Break</color>");
#endif
                    UnRegisterRemoteTerminal(recvConnId);
                    EvtDispatcher.Instance.Dispatch(NetworkEvt.EVT_ON_CONNECTION_BREAK, recvConnId, recvHostId);
                    break;

                case NetworkEventType.DataEvent:
                    // assembly and dispatch packet
                    m_Buffer.ReadHead();
                    IPacket packet = PacketFactory.AssemblyPacket(m_Buffer);
                    PacketDispatcher.Instance.DispatchPacket(packet);
                    break;

                case NetworkEventType.BroadcastEvent:
                    break;
                }
                ++processCnt;
            } while (evt != NetworkEventType.Nothing && processCnt < NetworkConst.MAX_PACKET_PROCESS_PER_FRAME);
        }
Exemplo n.º 2
0
        private RemoteTerminalInfo RegisterRemoteTerminal(int connID, int hostID)
        {
            if (m_ConnectedTerminalInfoMap.ContainsKey(connID))
            {
                Debug.LogError("Connection ID: " + connID + " duplicated!!!");
                return(null);
            }

            int    remotePort;
            ulong  remoteNetwork;
            ushort remoteDstNode;
            byte   error;
            string remoteIP = NetworkTransport.GetConnectionInfo(hostID, connID,
                                                                 out remotePort, out remoteNetwork, out remoteDstNode, out error);

            if (m_ConnectedIpSet.Contains(remoteIP))
            {
                Debug.LogError("Remote Terminal: " + remoteIP + " is connected already!");
                return(null);
            }

            RemoteTerminalInfo info = new RemoteTerminalInfo()
            {
                HostID = hostID,
                ConnID = connID,
                IP     = remoteIP,
                Port   = remotePort
            };

            m_ConnectedTerminalInfoMap.Add(connID, info);
            m_ConnectedIpSet.Add(remoteIP);

            EvtDispatcher.Instance.Dispatch(NetworkEvt.EVT_REMOTE_TERMINAL_REGISTERED, info);

            Debug.Log(string.Format("<color=blue>Remote Terminal {0} is connected to host {1} with Connection ID {2} via port {3}</color>",
                                    remoteIP, hostID, connID, remotePort));
            return(info);
        }