Exemplo n.º 1
0
        /// <summary>
        /// 添加UDP套接字
        /// </summary>
        /// <param name="name">套接字注册名称</param>
        /// <param name="ipPort">IP端口信息</param>
        /// <param name="remoteName">远程套接字注册名称</param>
        public void Add(byte[] name, ipPort ipPort, byte[] remoteName)
        {
            if (name == null || name.Length > udpServer.MaxPacketSize || remoteName == null || remoteName.Length > udpServer.MaxPacketSize)
            {
                log.Error.Throw(log.exceptionType.IndexOutOfRange);
            }
            udpSocket socket = null, oldSocket;

            try
            {
                socket = new udpSocket(this, name, ipPort, remoteName);
                Monitor.Enter(socketLock);
                try
                {
                    if (sockets.TryGetValue(ipPort, out oldSocket))
                    {
                        pub.Dispose(ref oldSocket);
                        sockets[ipPort] = socket;
                    }
                    else
                    {
                        sockets.Add(ipPort, socket);
                    }
                }
                finally { Monitor.Exit(socketLock); }
                socket.Start();
                socket = null;
            }
            finally { pub.Dispose(socket); }
        }
Exemplo n.º 2
0
 /// <summary>
 /// UDP穿透端口服务客户端
 /// </summary>
 /// <param name="client">UDP穿透端口服务客户端</param>
 /// <param name="name">套接字注册名称</param>
 /// <param name="ip">套接字绑定端口信息</param>
 /// <param name="remoteName">远程套接字注册名称</param>
 internal udpSocket(portClient client, byte[] name, ipPort ipPort, byte[] remoteName)
 {
     IpPort = ipPort;
     Socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
     Socket.Bind(new IPEndPoint(new IPAddress(ipPort.Ip), ipPort.Port));
     this.client     = client;
     this.name       = name;
     this.remoteName = new fastCSharp.code.cSharp.tcpBase.subByteArrayBuffer {
         Buffer = subArray <byte> .Unsafe(remoteName, 0, remoteName.Length)
     };
     timeout = date.NowSecond.AddTicks(client.timeoutTicks);
 }
Exemplo n.º 3
0
        /// <summary>
        /// 接受数据
        /// </summary>
        private unsafe void receive()
        {
            EndPoint endPoint = null;

            byte[] data = new byte[MaxPacketSize];
            fixed(byte *dataFixed = data)
            {
                while (socket != null)
                {
                    try
                    {
                        endPoint = anyIp;
                        int count = socket.ReceiveFrom(data, 0, MaxPacketSize, SocketFlags.None, ref endPoint);
                        if (count > 0)
                        {
                            ipPort oldPort, ipPort = (IPEndPoint)endPoint;
                            byte[] name = new byte[count];
                            Buffer.BlockCopy(data, 0, name, 0, count);
                            hashBytes nameKey = name;
                            if (names.TryGetValue(nameKey, out oldPort))
                            {
                                if (ipPort.Equals(oldPort))
                                {
                                    ports.RefreshTimeout(oldPort);
                                }
                                else
                                {
                                    ports.Remove(oldPort);
                                    ports[ipPort]  = nameKey;
                                    names[nameKey] = ipPort;
                                }
                            }
                            else
                            {
                                ports[ipPort]  = nameKey;
                                names[nameKey] = ipPort;
                            }
                        }
                    }
                    catch (Exception error)
                    {
                        Console.WriteLine(error.ToString());
                        Thread.Sleep(1);
                    }
                }
            }
        }
Exemplo n.º 4
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="ipPort"></param>
 /// <param name="name"></param>
 private void remove(ipPort ipPort, hashBytes name)
 {
     names.Remove(name);
 }