예제 #1
0
        public void Update()
        {
            if (client != null)
            {
                for (Packet packet = client.GetPacket(); packet != null; packet = client.GetPacket())
                {
                    PacketType packetType = packet.BeginRead();
                    EndPoint   remote     = null;
                    switch (type)
                    {
                    case ProtocolType.Tcp:
                    {
                        if (((Socket)packet.peer).Connected)
                        {
                            remote = ((Socket)packet.peer).RemoteEndPoint;
                        }
                        else
                        {
                            remote = new IPEndPoint(IPAddress.Any, 0);
                        }
                        break;
                    }

                    case ProtocolType.Udp:
                    {
                        remote = (EndPoint)packet.peer;
                        break;
                    }
                    }
                    if (listener != null)
                    {
                        switch (packetType)
                        {
                        case PacketType.CONNECT_SUCCESSFUL:
                        {
                            client.ConnectSuccessful(packet);
                            linkstate = LinkCobe.Connect;
                            listener.OnStatusChanged(linkstate);
                            break;
                        }

                        case PacketType.CONNECTION_LOST:
                        {
                            listener.DebugReturn(packet.state.ToString());
                            linkstate = LinkCobe.Lost;
                            listener.OnStatusChanged(linkstate);
                            client.Disconnect();
                            break;
                        }

                        case PacketType.CONNECTION_ATTEMPT_FAILED:
                        {
                            listener.DebugReturn(packet.state.ToString());
                            linkstate = LinkCobe.Failed;
                            listener.OnStatusChanged(linkstate);
                            client.Disconnect();
                            break;
                        }

                        case PacketType.ServerTell:
                        {
                            if (!string.IsNullOrEmpty(Key))
                            {
                                SendData sendData = packet.ReadSendData(Key);
                                listener.OnEvent(sendData);
                            }
                            break;
                        }

                        case PacketType.Response:
                        {
                            if (!string.IsNullOrEmpty(Key))
                            {
                                SendData sendData = packet.ReadSendData(Key);
                                listener.OnOperationResponse(sendData);
                            }
                            break;
                        }

                        case PacketType.P2P_CONNECT_SUCCESSFUL:
                        {
                            if (!P2PToPeer.ContainsKey(remote))
                            {
                                listener.DebugReturn("P2PConnectSuccess: " + remote.ToString());
                                client.P2PConnectSuccessful(listener.P2PAddPeer, packet);
                            }
                            break;
                        }

                        case PacketType.P2P_CONNECTION_LOST:
                        {
                            if (P2PToPeer.ContainsKey(remote))
                            {
                                PeerForP2PBase peer = P2PToPeer[remote];
                                listener.DebugReturn("P2PConnectLost:" + remote.ToString() + ", error: " + packet.state);
                                peer.OnDisconnect();
                                client.P2PDisconnect(packet.peer);
                                peer = null;
                            }
                            break;
                        }

                        case PacketType.P2P_Tell:
                        {
                            if (P2PToPeer.ContainsKey(remote))
                            {
                                PeerForP2PBase peer     = P2PToPeer[remote];
                                SendData       sendData = packet.ReadSendData(client.P2PSocketToKey[remote]);
                                peer.OnGetData(sendData);
                            }
                            break;
                        }
                        }
                    }
                    packet.CloseStream();
                }
            }
        }