Esempio n. 1
0
 internal Boolean SetBuffer(SocketAsyncEventArgs args)
 {
     try
     {
         if (_FreeIndexPool.Count > 0)
         {
             args.SetBuffer(_Buffer, _FreeIndexPool.Pop(), _BufferSize);
         }
         else
         {
             if ((_NumBytes - _BufferSize) < _CurrentIndex)
             {
                 return(false);
             }
             args.SetBuffer(_Buffer, _CurrentIndex, _BufferSize);
             _CurrentIndex += _BufferSize;
         }
     }
     catch (Exception ex)
     {
         AsyncUserToken token = args.UserToken as AsyncUserToken;
         log.AddLog(LogType.Error, $"在SetBuffer时出错!\r\n客户端地址:{token.IPAddress}\r\n错误信息:{ex.Message}");
     }
     return(true);
 }
Esempio n. 2
0
        private void SocketManager_ReceiveClientData(AsyncUserToken token, byte[] buff)
        {
            var msg = String.Empty;

            try
            {
                msg = Encoding.UTF8.GetString(buff);
                var item = JsonConvert.DeserializeObject <JsonItem>(msg);
                switch (item.State)
                {
                case "Add":
                    AddItem(token, item);
                    break;

                case "Del":
                    DelItem(token, item);
                    break;

                case "Que":
                    QueItem(token);
                    break;

                default:
                    _SocketManager.SendMessage(token, new byte[] { 85, 110, 107, 110, 111, 119, 83, 116, 97, 116, 101 });     // UnknowState
                    log.AddLog(LogType.Info, $"用户{token.IPAddress}发送了错误的Json信息!");
                    break;
                }
            }
            catch (Exception ex)
            {
                log.AddLog(LogType.Error, $"在执行解析内容时出错!\r\n发送用户:{token.IPAddress}\r\n发送内容:{msg}\r\n错误信息:{ex.Message}");
                _SocketManager.SendMessage(token, new byte[] { 68, 101, 115, 101, 114, 105, 97, 108, 105, 122, 101, 74, 115, 111, 110, 69, 114, 114, 111, 114 }); // DeserializeJsonError
            }
        }
Esempio n. 3
0
        internal SocketManager(Int32 numConnections, Int32 receiveBufferSize, ref LogMain logMain)
        {
            log          = logMain;
            _ClientCount = 0;                   // 清理客户端连接数

            _MaxConnectNum = numConnections;    // 设置最大客户端连接数
            _RevBufferSize = receiveBufferSize; // 设置缓冲区大小
            try
            {
                _BufferManager = new BufferManager(receiveBufferSize * numConnections * opsToAlloc, receiveBufferSize, ref log); // 分配缓冲区,
                // 使得最大数量套接字可以有一个未完成读写同时发送到Socket
                _Pool = new SocketEventPool(numConnections);                                                                     // 初始化套接字事件池
                _MaxNumberAcceptedClients = new Semaphore(numConnections, numConnections);                                       // 限制I/O大小范围
            }
            catch (Exception ex)
            {
                log.AddLog(LogType.Error, $"因用户操作不当,此Bug已影响程序正常运行!\r\n错误信息:{ex.Message}");
            }
        }
Esempio n. 4
0
 internal Boolean Start(IPEndPoint localEndPoint)
 {
     try
     {
         _Clients.Clear();                                                                             // 清空用户
         _ListenSocket = new Socket(localEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); // 初始化Socket新实例
         _ListenSocket.Bind(localEndPoint);                                                            // 绑定终结点
         _ListenSocket.Listen(_MaxConnectNum);                                                         // 设置最大客户端数量
         StartAccept(null);
         return(true);
     }
     catch (Exception ex)
     {
         log.AddLog(LogType.Error, $"监听端口时出错!\r\n错误信息:{ex.Message}");
         return(false);
     }
 }