예제 #1
0
파일: TcpServer.cs 프로젝트: mkjmmc/WeiBar
 void DisposeSocket(Socket Socket)
 {
     lock (socketDic)
     {
         try
         {
             Socket.Shutdown(SocketShutdown.Both);
         }
         catch { }
         if (socketDic.ContainsKey(Socket))
         {
             SocketModel socketModel = socketDic[Socket];
             setBufData(socketModel.readEvent.Buffer);
             socketModel.readEvent.Completed  -= readEvent_Completed;
             socketModel.writeEvent.Completed -= writeEvent_Completed;
             socketModel.readEvent.Dispose();
             socketModel.writeEvent.Dispose();
             //readEventPool.Push(socketModel.readEvent);
             //writeEventPool.Push(socketModel.writeEvent);
             socketDic.Remove(Socket);
         }
         Socket.Close();
         Socket.Dispose();
     }
 }
예제 #2
0
파일: TcpServer.cs 프로젝트: mkjmmc/WeiBar
 public void SendData(byte Type, Socket Socket, byte[] Data)
 {
     lock (socketDic)
     {
         if (!socketDic.ContainsKey(Socket))
         {
             return;
         }
         SocketModel socketModel = socketDic[Socket];
         try
         {
             byte[] _sendData = new byte[Data.Length + 9];
             _sendData[8] = Type;
             Array.Copy(Data, 0, _sendData, 9, Data.Length);
             Array.Copy(headByte, 0, _sendData, 0, 8);
             Array.Copy(BitConverter.GetBytes(Data.Length + 1), 0, _sendData, 4, 4);
             int _dataLength = _sendData.Length;
             if (_dataLength <= 65535)
             {
                 socketModel.writeDataBuf.Add(_sendData);
             }
             else
             {
                 int _readNum = 0;
                 while (true)
                 {
                     if (_dataLength - _readNum <= 65535)
                     {
                         byte[] _temp = new byte[_dataLength - _readNum];
                         Array.Copy(_sendData, _readNum, _temp, 0, _dataLength - _readNum);
                         socketModel.writeDataBuf.Add(_temp);
                         break;
                     }
                     else
                     {
                         byte[] _temp = new byte[65535];
                         Array.Copy(_sendData, _readNum, _temp, 0, 65535);
                         socketModel.writeDataBuf.Add(_temp);
                         _readNum += 65535;
                     }
                 }
             }
             if (!socketModel.writeType)
             {
                 socketModel.writeType = true;
                 byte[] _sendTempData = socketModel.writeDataBuf[0];
                 socketModel.writeDataBuf.RemoveAt(0);
                 SocketAsyncEventArgs _socketAsyncEventArgs = socketModel.writeEvent;
                 _socketAsyncEventArgs.SetBuffer(_sendTempData, 0, _sendTempData.Length);
                 bool isOK = Socket.SendAsync(_socketAsyncEventArgs);
                 if (!isOK)
                 {
                     writeEvent_Completed(null, _socketAsyncEventArgs);
                 }
             }
         }
         catch { }
     }
 }
예제 #3
0
파일: TcpServer.cs 프로젝트: mkjmmc/WeiBar
        //void message_Run()
        //{
        //    while (true)
        //    {
        //        lock (messagePool)
        //        {

        //            foreach (var _v in messagePool)
        //            {
        //                if (_v.Value.Count > messageNum)
        //                {
        //                    _v.Key.Close();
        //                    continue;
        //                }
        //                if (_v.Value.Count > 0)
        //                {
        //                    try
        //                    {
        //                        //if (GetProcessor() <= 80)
        //                        //{
        //                        //threadNowNum++;
        //                        ThreadPool.QueueUserWorkItem(ReadOK, new object[] { _v.Key, _v.Value[0] });
        //                        _v.Value.RemoveAt(0);
        //                        //}

        //                    }
        //                    catch { }
        //                }
        //            }

        //        }
        //        Thread.Sleep(messageTime);
        //    }
        //}
        void connectEvent_Completed(object sender, SocketAsyncEventArgs e)
        {
            lock (socketDic)
            {
                SocketModel socketModel = new SocketModel();
                try
                {
                    socketModel.socket = e.AcceptSocket;
                    //_socket.SendBufferSize = 65535;
                    //_socket.ReceiveBufferSize = 65535;
                    socketModel.socket.NoDelay = true;
                    socketModel.readEvent      = readEventPool.Pop();
                    if (socketModel.readEvent == null)
                    {
                        socketModel.readEvent = new SocketAsyncEventArgs();
                        byte[] _buf = getBufData();
                        if (_buf == null)
                        {
                            _buf = new byte[4096];
                        }
                        socketModel.readEvent.DisconnectReuseSocket = false;
                        socketModel.readEvent.SetBuffer(_buf, 0, _buf.Length);
                        socketModel.readEvent.Completed += readEvent_Completed;
                    }
                    socketModel.readEvent.UserToken = socketModel.socket;
                    socketModel.writeEvent          = writeEventPool.Pop();
                    if (socketModel.writeEvent == null)
                    {
                        socketModel.writeEvent = new SocketAsyncEventArgs();
                        socketModel.writeEvent.DisconnectReuseSocket = false;
                        socketModel.writeEvent.Completed            += writeEvent_Completed;
                    }
                    socketModel.writeEvent.UserToken = socketModel.socket;
                    socketModel.writeType            = false;
                    socketModel.writeDataBuf         = new List <byte[]>();
                    //socketModel.readDataBuf = new ReadDataModel();
                    // messageTime.Add(_socket, 0L);
                    connectErrorNum = 0;
                    ThreadPool.QueueUserWorkItem(ConnectOK, new object[] { socketModel.socket });
                    e.AcceptSocket = null;
                    socketDic.Add(socketModel.socket, socketModel);
                    bool isReceiveOK = socketModel.socket.ReceiveAsync(socketModel.readEvent);
                    if (!isReceiveOK)
                    {
                        readEvent_Completed(null, socketModel.readEvent);
                    }
                    bool isOK = socket.AcceptAsync(e);
                    if (!isOK)
                    {
                        connectEvent_Completed(socket, e);
                    }
                }
                catch (Exception ex)
                {
                    try
                    {
                        StreamWriter _sw = new StreamWriter(@"c:\SocketError.txt", true);
                        _sw.WriteLine("-------------------" + DateTime.Now.ToString() + "-----------------------");
                        _sw.WriteLine(ex.ToString());
                        _sw.Close();
                        if (socketModel.readEvent != null)
                        {
                            //_readEventx.AcceptSocket = null;
                            readEventPool.Push(socketModel.readEvent);
                        }
                        if (socketModel.writeEvent != null)
                        {
                            writeEventPool.Push(socketModel.writeEvent);
                        }
                        if (socketModel.socket != null)
                        {
                            DisposeSocket(socketModel.socket);
                        }
                    }
                    catch { }
                }
            }
        }