Esempio n. 1
0
        public void SendEx(IocpUserToken token, byte[] buffer)
        {
            byte[] encrypted = AESEx.AESEncrypt(buffer, CommonConfig.key, CommonConfig.iv);

            MemoryStream st = new MemoryStream();
            BinaryWriter bw = new BinaryWriter(st);

            bw.Write(encrypted.Length + sizeof(int) + sizeof(ushort));
            bw.Write(Shared.PackageSignature);
            bw.Write(encrypted);
            RealSend(token, BufferEx.MemoryStreamToBytes(st));
        }
Esempio n. 2
0
        public void SendEx(IocpUserToken token, byte[] buffer)
        {
            byte[] encrypted = AESEx.AESEncrypt(buffer, CommonConfig.key, CommonConfig.iv);

            MemoryStream st = new MemoryStream();
            BinaryWriter bw = new BinaryWriter(st);

            bw.Write(encrypted.Length + sizeof(int) + sizeof(ushort));
            bw.Write(Shared.PackageSignature);
            bw.Write(encrypted);
            RealSend(token, BufferEx.MemoryStreamToBytes(st));
        }
Esempio n. 3
0
        /// <summary>
        /// 发送数据包给客户
        /// </summary>
        /// <param name="token"></param>
        /// <param name="sendBuffer"></param>
        public void RealSend(IocpUserToken token, byte[] sendBuffer)
        {
            SocketAsyncEventArgs args = GetSocketAsyncEventArgs();

            args.SetBuffer(sendBuffer, 0, sendBuffer.Length);
            args.UserToken = token;
            try
            {
                if (!token.Connection.SendAsync(args))
                {
                    OnClientIoSend(args);
                }
            }
            catch (Exception)
            {
                CloseClientSocket(args);                        //关闭一个套接字
            }
        }
Esempio n. 4
0
        /// <summary>
        /// 当这个被调用时,代表一个异步接收操作完成。
        /// 处理新用户的连接请求。
        /// </summary>
        /// <param name="e">SocketAsyncEventArg表示一个已经完成的异步IO事件。</param>
        private void ProcessAccept(SocketAsyncEventArgs e)
        {
            Socket s = e.AcceptSocket;

            if (s.Connected)
            {
                SocketAsyncEventArgs readEventArgs = this.GetSocketAsyncEventArgs();
                try
                {
                    IocpUserToken token = new IocpUserToken(s, Interlocked.Increment(ref unique_id));
                    readEventArgs.UserToken = token;

                    //增加当前服务器的连接数量计数器。
                    Interlocked.Increment(ref this.numConnectedSockets);

                    //添加用户连接到表
                    lock (tokenMap)
                    {
                        tokenMap[token.UniqueId] = token;
                    }

                    if (NotifyNewConnection != null)
                    {
                        NotifyNewConnection(this, new IocpNewConnectionArgs(token));
                    }

                    if (!s.ReceiveAsync(readEventArgs))
                    {
                        this.OnClientIoReceive(readEventArgs);
                    }
                }
                catch (Exception)
                {
                    CloseClientSocket(readEventArgs);
                }

                // 接收下一个连接请求
                this.PostIoAccept(e);
            }
            else
            {
                s.Close();
            }
        }
Esempio n. 5
0
        /// <summary>
        /// 关闭一个连接。
        /// </summary>
        /// <param name="token">用户关联的IocpUserToken结构</param>
        /// <param name="e">与发送接收相关的SocketAsyncEventArgs结构</param>
        private void CloseClientSocket(IocpUserToken token, SocketAsyncEventArgs e)
        {
            lock (tokenMap)
            {
                if (tokenMap.Remove(token.UniqueId))
                {
                    //通知用户断开了连接
                    if (NotifyDisconnectConnection != null)
                    {
                        NotifyDisconnectConnection(this, new IocpDisconnectConnectionArgs(token));
                    }

                    // 减少总连接数
                    Interlocked.Decrement(ref this.numConnectedSockets);

                    token.Connection.Close();
                }
            }

            // 释放SocketAsyncEventArgs结构,以方便让其他套接字重用该结构
            this.ReleaseSocketAsyncEventArgs(e);
        }
Esempio n. 6
0
        /// <summary>
        /// 当这个被调用时,代表一个异步接收操作完成。
        /// 当远程主机关闭连接,这个Socket也将被关闭
        /// 如果有数据接收,那么就会执行事件通知用户
        /// </summary>
        /// <param name="e">SocketAsyncEventArg表示一个已经完成的异步IO事件。</param>
        private void OnClientIoReceive(SocketAsyncEventArgs e)
        {
            //获取与用户关联的Token结构
            IocpUserToken token = e.UserToken as IocpUserToken;
            //获取用户的套接字
            Socket s = token.Connection;

            // 检查是否是数据接收
            // 是否非错误并且SOCKET是有效的

            try
            {
                if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success && s.Available == 0)
                {
                    if (NotifyReceivedPackage != null)
                    {
                        MemoryStream TempBuffer = new MemoryStream();
                        new BinaryWriter(TempBuffer).Write(e.Buffer, 0, e.BytesTransferred);

                        NotifyReceivedPackage(this, new IocpPacketEventArgs(token, BufferEx.MemoryStreamToBytes(TempBuffer)));
                    }
                    //投递下一个接收函数
                    if (!s.ReceiveAsync(e))
                    {
                        // 如果投递失败,检查是否是成功接收
                        this.OnClientIoReceive(e);
                    }
                }
                else
                {
                    throw new SocketException(10054);                    //WSAECONNRESET
                }
            }
            catch (Exception)
            {
                this.CloseClientSocket(e);
            }
        }
Esempio n. 7
0
        /// <summary>
        /// 当这个被调用时,代表一个异步接收操作完成。
        /// 当远程主机关闭连接,这个Socket也将被关闭
        /// 如果没有错误,那么就会执行事件通知用户发送的字节数以及数据
        /// </summary>
        /// <param name="e">SocketAsyncEventArg表示一个已经完成的异步IO事件。</param>
        private void OnClientIoSend(SocketAsyncEventArgs e)
        {
            //获取与用户关联的Token结构
            IocpUserToken token = e.UserToken as IocpUserToken;
            //获取用户的套接字
            Socket s = token.Connection;

            if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)
            {
                if (NotifySendComplete != null)
                {
                    MemoryStream TempBuffer = new MemoryStream();
                    new BinaryWriter(TempBuffer).Write(e.Buffer, 0, e.BytesTransferred);
                    NotifySendComplete(this, new IocpPacketEventArgs(token, BufferEx.MemoryStreamToBytes(TempBuffer)));
                }

                ReleaseSocketAsyncEventArgs(e);
            }
            else
            {
                this.CloseClientSocket(e);
            }
        }
Esempio n. 8
0
        /// <summary>
        /// 关闭一个连接。
        /// </summary>
        /// <param name="e">与发送接收相关的SocketAsyncEventArgs结构</param>
        public void CloseClientSocket(SocketAsyncEventArgs e)
        {
            IocpUserToken token = e.UserToken as IocpUserToken;

            this.CloseClientSocket(token, e);
        }
Esempio n. 9
0
 /// <summary>
 /// 发送数据包给客户
 /// </summary>
 /// <param name="token"></param>
 /// <param name="sendBuffer"></param>
 public void RealSend(IocpUserToken token, byte[] sendBuffer)
 {
     SocketAsyncEventArgs args = GetSocketAsyncEventArgs();
     args.SetBuffer(sendBuffer, 0, sendBuffer.Length);
     args.UserToken = token;
     try
     {
         if (!token.Connection.SendAsync(args))
         {
             OnClientIoSend(args);
         }
     }
     catch (Exception)
     {
         CloseClientSocket(args);        //关闭一个套接字
     }
 }
Esempio n. 10
0
 public IocpDisconnectConnectionArgs(IocpUserToken token)
 {
     this.token = token;
 }
Esempio n. 11
0
 public IocpDisconnectConnectionArgs(IocpUserToken token)
 {
     this.token = token;
 }
Esempio n. 12
0
 public IocpNewConnectionArgs(IocpUserToken token)
 {
     this.token = token;
 }
Esempio n. 13
0
        private void ProcessError(SocketAsyncEventArgs e)
        {
            IocpUserToken token = e.UserToken as IocpUserToken;

            this.CloseClientSocket(token, e);
        }
Esempio n. 14
0
 public NetworkComputeBuffer(IocpUserToken _instance)
 {
     this.token       = _instance;
     BytesTransferred = 0;
     TotalBuffer      = new MemoryStream();
 }
Esempio n. 15
0
 public USER_NetworkReceivedArgs(IocpUserToken token, byte[] buffer)
 {
     this.token = token;
     this.buffer = buffer;
 }
Esempio n. 16
0
 public IocpPacketEventArgs(IocpUserToken token, byte[] buffer)
 {
     this.token  = token;
     this.Buffer = buffer;
 }
Esempio n. 17
0
 public IocpPacketEventArgs(IocpUserToken token, byte[] buffer)
 {
     this.token = token;
     this.Buffer = buffer;
 }
Esempio n. 18
0
        /// <summary>
        /// 当这个被调用时,代表一个异步接收操作完成。
        /// 处理新用户的连接请求。
        /// </summary>
        /// <param name="e">SocketAsyncEventArg表示一个已经完成的异步IO事件。</param>
        private void ProcessAccept(SocketAsyncEventArgs e)
        {
            Socket s = e.AcceptSocket;
            if (s.Connected)
            {
                SocketAsyncEventArgs readEventArgs = this.GetSocketAsyncEventArgs();
                try
                {
                    IocpUserToken token = new IocpUserToken(s, Interlocked.Increment(ref unique_id));
                    readEventArgs.UserToken = token;

                    //增加当前服务器的连接数量计数器。
                    Interlocked.Increment(ref this.numConnectedSockets);

                    //添加用户连接到表
                    lock (tokenMap)
                    {
                        tokenMap[token.UniqueId] = token;
                    }

                    if (NotifyNewConnection != null)
                    {
                        NotifyNewConnection(this, new IocpNewConnectionArgs(token));
                    }

                    if (!s.ReceiveAsync(readEventArgs))
                    {
                        this.OnClientIoReceive(readEventArgs);
                    }
                }
                catch (Exception)
                {
                    CloseClientSocket(readEventArgs);
                }

                // 接收下一个连接请求
                this.PostIoAccept(e);
            }
            else
            {
                s.Close();
            }
        }
Esempio n. 19
0
 public IocpNewConnectionArgs(IocpUserToken token)
 {
     this.token = token;
 }
Esempio n. 20
0
        /// <summary>
        /// 关闭一个连接。
        /// </summary>
        /// <param name="token">用户关联的IocpUserToken结构</param>
        /// <param name="e">与发送接收相关的SocketAsyncEventArgs结构</param>
        private void CloseClientSocket(IocpUserToken token, SocketAsyncEventArgs e)
        {
            lock (tokenMap)
            {
                if (tokenMap.Remove(token.UniqueId))
                {
                    //通知用户断开了连接
                    if (NotifyDisconnectConnection != null)
                    {
                        NotifyDisconnectConnection(this, new IocpDisconnectConnectionArgs(token));
                    }

                    // 减少总连接数
                    Interlocked.Decrement(ref this.numConnectedSockets);

                    token.Connection.Close();
                }
            }

            // 释放SocketAsyncEventArgs结构,以方便让其他套接字重用该结构
            this.ReleaseSocketAsyncEventArgs(e);
        }
Esempio n. 21
0
 public USER_NetworkReceivedArgs(IocpUserToken token, byte[] buffer)
 {
     this.token  = token;
     this.buffer = buffer;
 }
Esempio n. 22
0
 public NetworkComputeBuffer(IocpUserToken _instance)
 {
     this.token = _instance;
     BytesTransferred = 0;
     TotalBuffer = new MemoryStream();
 }