Beispiel #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); }
        }
Beispiel #2
0
        /// <summary>
        /// UDP套接字连接完成事件
        /// </summary>
        /// <param name="socket">UDP套接字</param>
        private void onSocket(udpSocket socket)
        {
            udpSocket oldSocket;

            Monitor.Enter(socketLock);
            try
            {
                if (sockets.TryGetValue(socket.IpPort, out oldSocket) && socket == oldSocket)
                {
                    sockets.Remove(socket.IpPort);
                }
            }
            finally { Monitor.Exit(socketLock); }
            if (!socket.IsReceive)
            {
                socket.Dispose();
            }
            if (OnUdpSocket != null)
            {
                OnUdpSocket(socket);
            }
        }