Example #1
0
 public void Disconnect(object socket, int timeout)
 {
     if (socket != null)
     {
         if (ToPeer.ContainsKey((EndPoint)socket))
         {
             if (SocketList.Contains(ToPeer[(EndPoint)socket]))
             {
                 SocketList.Remove(ToPeer[(EndPoint)socket]);
             }
         }
         if (SocketToKey.ContainsKey((EndPoint)socket))
         {
             SocketToKey.Remove((EndPoint)socket);
         }
         if (ToPeer.ContainsKey((EndPoint)socket))
         {
             ToPeer.Remove((EndPoint)socket);
         }
         lock (checklock)
         {
             if (clientcheck.ContainsKey((EndPoint)socket))
             {
                 clientcheck.Remove((EndPoint)socket);
             }
         }
     }
 }
Example #2
0
        public void Disconnect(object socket, int timeout)
        {
            if (socket != null)
            {
                if (SocketToEndPoint.ContainsKey((Socket)socket))
                {
                    if (ToPeer.ContainsKey(SocketToEndPoint[(Socket)socket]))
                    {
                        if (SocketList.Contains(ToPeer[SocketToEndPoint[(Socket)socket]]))
                        {
                            SocketList.Remove(ToPeer[SocketToEndPoint[(Socket)socket]]);
                        }
                    }
                    if (SocketToKey.ContainsKey(socket))
                    {
                        SocketToKey.Remove(socket);
                    }
                    if (ToPeer.ContainsKey(SocketToEndPoint[(Socket)socket]))
                    {
                        ToPeer.Remove(SocketToEndPoint[(Socket)socket]);
                    }
                }

                try
                {
                    ((Socket)socket).Shutdown(SocketShutdown.Both);
                }
                catch (Exception e)
                {
                    GetMessage(e.Message);
                }
                finally
                {
                    if (timeout == -1)
                    {
                        ((Socket)socket).Close();
                    }
                    else
                    {
                        ((Socket)socket).Close(timeout);
                    }
                    socket = null;
                }
            }
        }
Example #3
0
        private void Receive(System.IAsyncResult ar)
        {
            EndPoint ipe = new IPEndPoint(IPAddress.IPv6Any, 0);

            byte[]     data   = null;
            PacketType msgid  = (PacketType)(-1);
            Packet     packet = null;

            try
            {
                int num = listener.EndReceiveFrom(ar, ref ipe);
                if (num >= 4)
                {
                    data = new byte[num];
                    Buffer.BlockCopy(m_Buffer, 0, data, 0, num);
                    if (BitConverter.ToInt32(data, 0) == data.Length - 4)
                    {
                        packet = new Packet(ipe, data, null);
                        msgid  = packet.BeginRead();
                        packet.ResetPosition();
                    }
                }
                EndPoint remoteEP = new IPEndPoint(IPAddress.IPv6Any, 0);
                listener.BeginReceiveFrom(m_Buffer, 0, m_Buffer.Length, SocketFlags.None, ref remoteEP, Receive, listener);
                if (packet == null)
                {
                    return;
                }
            }
            catch (SocketException e)
            {
                DebugMessage("Server Close On Receive:" + e.ToString());
                Close();
                return;
            }
            catch (Exception e)
            {
                DebugMessage("Receive:" + e.ToString());
                EndPoint remoteEP = new IPEndPoint(IPAddress.IPv6Any, 0);
                listener.BeginReceiveFrom(m_Buffer, 0, m_Buffer.Length, SocketFlags.None, ref remoteEP, Receive, listener);
                return;
            }
            lock (OnListenClient)
            {
                if (OnListenClient.ContainsKey((EndPoint)packet.peer))
                {
                    OnListenClient[(EndPoint)packet.peer].Add(packet);
                    return;
                }
            }
            if (msgid > PacketType.SendAllowTypeTop && msgid < PacketType.SendAllowTypeEnd && ToPeer.ContainsKey((EndPoint)packet.peer))
            {
                PushPacket(packet);
                lock (checklock)
                {
                    clientcheck[(EndPoint)packet.peer] = true;
                }
            }
            else
            {
                switch (msgid)
                {
                case PacketType.ON_CONNECT:
                {
                    ListenClient(packet);
                    break;
                }

                case PacketType.CONNECTION_LOST:
                {
                    if (ToPeer.ContainsKey((EndPoint)packet.peer))
                    {
                        packet.BeginRead();
                        SendData sendData = packet.ReadSendData("");
                        PushPacket(PacketType.CONNECTION_LOST, sendData.DebugMessage, packet.peer);
                    }
                    packet.CloseStream();
                    break;
                }

                case PacketType.P2P_SERVER_CALL:
                {
                    if (ToPeer.ContainsKey((EndPoint)packet.peer))
                    {
                        packet.BeginRead();
                        SendData sendData = packet.ReadSendData(SocketToKey[packet.peer]);

                        string remoteendpoint = (string)((object[])sendData.Parameters)[0];
                        string localendpoint  = (string)((object[])sendData.Parameters)[1];
                        object thedata        = ((object[])sendData.Parameters)[2];

                        if (remoteendpoint != packet.peer.ToString() && ToPeer.TryGetValue(TraceRoute.IPEndPointParse(remoteendpoint, AddressFamily.InterNetworkV6), out PeerBase peer))
                        {
                            ((object[])sendData.Parameters)[0] = packet.peer.ToString();
                            ((object[])sendData.Parameters)[1] = remoteendpoint;
                            using (Packet sendpacket = new Packet(peer.socket))
                            {
                                sendpacket.BeginWrite(PacketType.P2P_SERVER_CALL);
                                sendpacket.WriteSendData(sendData, SocketToKey[TraceRoute.IPEndPointParse(remoteendpoint, AddressFamily.InterNetworkV6)], EncryptAndCompress.LockType.AES);
                                Send(sendpacket, peer.socket);
                            }
                            //DebugMessage(((Client.P2PCode)sendData.Code).ToString() + ":" + packet.peer.ToString() + " to " + remoteendpoint.ToString());
                        }
                        else
                        {
                            ((object[])sendData.Parameters)[1] = packet.peer.ToString();
                            using (Packet sendpacket = new Packet(packet.peer))
                            {
                                sendpacket.BeginWrite(PacketType.P2P_SERVER_FAILED);
                                sendpacket.WriteSendData(sendData, SocketToKey[packet.peer], EncryptAndCompress.LockType.AES);
                                Send(sendpacket, packet.peer);
                            }
                        }
                    }
                    packet.CloseStream();
                    break;
                }

                case PacketType.P2P_GET_PUBLIC_ENDPOINT:
                {
                    using (Packet sendpacket = new Packet(packet.peer))
                    {
                        sendpacket.BeginWrite(PacketType.P2P_GET_PUBLIC_ENDPOINT);
                        sendpacket.WriteSendData(new SendData(0, packet.peer.ToString()), "", EncryptAndCompress.LockType.None);
                        Send(sendpacket, packet.peer);
                    }
                    packet.CloseStream();
                    break;
                }

                case PacketType.CHECK:
                {
                    if (ToPeer.ContainsKey((EndPoint)packet.peer))
                    {
                        lock (checklock)
                        {
                            clientcheck[(EndPoint)packet.peer] = true;
                        }
                    }
                    packet.CloseStream();
                    break;
                }

                case PacketType.ClientDebugMessage:
                {
                    if (ToPeer.ContainsKey((EndPoint)packet.peer))
                    {
                        packet.BeginRead();
                        SendData sendData = packet.ReadSendData(SocketToKey[packet.peer]);
                        DebugMessage("ClientDebugMessage:" + packet.peer.ToString() + " " + sendData.DebugMessage);
                    }

                    packet.CloseStream();
                    break;
                }

                default:
                {
                    DebugMessage(msgid.ToString());
                    //PushPacket(PacketType.CONNECTION_LOST, "不正確的標頭資訊 Receive", packet.peer);
                    packet.CloseStream();
                    break;
                }
                }
            }
        }