Exemplo n.º 1
0
 /// <summary>
 /// 触发网络错误事件
 /// </summary>
 /// <param name="state"></param>
 private void RaiseNetError(SocketClientHandle handle)
 {
     if (NetError != null)
     {
         NetError(this, new SocketEventArgs(handle));
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// 开始进行监听
        /// </summary>
        private void StartListen()
        {
            _serverSock.Listen(1024);
            SocketClientHandle handle;

            while (IsRunning)
            {
                if (_clientCount >= _maxClient)
                {
                    //TODO 客户端过多异常
                    RaiseOtherException(null);
                }
                else
                {
                    Socket clientSock = _serverSock.Accept();
                    _clientCount++;
                    IPEndPoint ip = (IPEndPoint)clientSock.RemoteEndPoint;
                    Console.WriteLine("获取到来自{0} : {1}的连接", ip.Address, ip.Port);
                    //TODO 创建一个处理客户端的线程并启动
                    handle = new SocketClientHandle(clientSock);
                    _clients.Add(handle);

                    //使用线程池来操作
                    ThreadPool.QueueUserWorkItem(new WaitCallback(handle.RecevieData));

                    //Thread pthread;
                    //pthread = new Thread(new ThreadStart(client.RecevieData));
                    //pthread.Start();
                    //这里应该使用线程池来进行
                }
            }
        }
Exemplo n.º 3
0
 private void RaiseDataReceived(SocketClientHandle handle)
 {
     if (DataReceived != null)
     {
         DataReceived(this, new SocketEventArgs(handle));
     }
 }
Exemplo n.º 4
0
 /// <summary>
 /// 触发数据发送事件
 /// </summary>
 /// <param name="state"></param>
 private void RaiseCompletedSend(SocketClientHandle handle)
 {
     if (CompletedSend != null)
     {
         CompletedSend(this, new SocketEventArgs(handle));
     }
 }
Exemplo n.º 5
0
 /// <summary>
 /// 触发异常事件
 /// </summary>
 /// <param name="state"></param>
 private void RaiseOtherException(SocketClientHandle handle, string descrip)
 {
     if (OtherException != null)
     {
         OtherException(this, new SocketEventArgs(descrip, handle));
     }
 }
Exemplo n.º 6
0
 /// <summary>
 /// 触发客户端连接事件
 /// </summary>
 /// <param name="state"></param>
 private void RaiseClientConnected(SocketClientHandle handle)
 {
     if (ClientConnected != null)
     {
         ClientConnected(this, new SocketEventArgs(handle));
     }
 }
Exemplo n.º 7
0
 /// <summary>
 /// 关闭一个与客户端之间的会话
 /// </summary>
 /// <param name="handle">需要关闭的客户端会话对象</param>
 public void Close(SocketClientHandle handle)
 {
     if (handle != null)
     {
         _clients.Remove(handle);
         handle.Dispose();
         _clientCount--;
         //TODO 触发关闭事件
     }
 }
Exemplo n.º 8
0
 private void RaiseOtherException(SocketClientHandle handle)
 {
     RaiseOtherException(handle, "");
 }
Exemplo n.º 9
0
 /// <summary>
 /// 发送函数
 /// </summary>
 public void Send(string msg, SocketClientHandle client)
 {
     //TODO
 }
Exemplo n.º 10
0
 public SocketEventArgs(string msg, SocketClientHandle handle)
 {
     this._msg = msg;
     this._handle = handle;
     IsHandled = false;
 }
Exemplo n.º 11
0
 public SocketEventArgs(SocketClientHandle handle)
 {
     this._handle = handle;
     IsHandled    = false;
 }
Exemplo n.º 12
0
 /// <summary>
 /// 触发网络错误事件
 /// </summary>
 /// <param name="state"></param>
 private void RaiseNetError(SocketClientHandle handle)
 {
     if (NetError != null)
     {
         NetError(this, new SocketEventArgs(handle));
     }
 }
Exemplo n.º 13
0
 /// <summary>
 /// 触发异常事件
 /// </summary>
 /// <param name="state"></param>
 private void RaiseOtherException(SocketClientHandle handle, string descrip)
 {
     if (OtherException != null)
     {
         OtherException(this, new SocketEventArgs(descrip, handle));
     }
 }
Exemplo n.º 14
0
 private void RaiseDataReceived(SocketClientHandle handle)
 {
     if (DataReceived != null)
     {
         DataReceived(this, new SocketEventArgs(handle));
     }
 }
Exemplo n.º 15
0
 /// <summary>
 /// 触发数据发送事件
 /// </summary>
 /// <param name="state"></param>
 private void RaiseCompletedSend(SocketClientHandle handle)
 {
     if (CompletedSend != null)
     {
         CompletedSend(this, new SocketEventArgs(handle));
     }
 }
Exemplo n.º 16
0
 /// <summary>
 /// 触发客户端连接事件
 /// </summary>
 /// <param name="state"></param>
 private void RaiseClientConnected(SocketClientHandle handle)
 {
     if (ClientConnected != null)
     {
         ClientConnected(this, new SocketEventArgs(handle));
     }
 }
Exemplo n.º 17
0
 /// <summary>
 /// 发送函数
 /// </summary>
 public void Send(string msg, SocketClientHandle client)
 {
     //TODO
 }
Exemplo n.º 18
0
        /// <summary>
        /// 关闭一个与客户端之间的会话
        /// </summary>
        /// <param name="handle">需要关闭的客户端会话对象</param>
        public void Close(SocketClientHandle handle)
        {
            if (handle != null)
            {
                _clients.Remove(handle);
                handle.Dispose();
                _clientCount--;
                //TODO 触发关闭事件

            }
        }
Exemplo n.º 19
0
 private void RaiseOtherException(SocketClientHandle handle)
 {
     RaiseOtherException(handle, "");
 }
Exemplo n.º 20
0
        /// <summary>
        /// 开始进行监听
        /// </summary>
        private void StartListen()
        {
            _serverSock.Listen(1024);
            SocketClientHandle handle;
            while (IsRunning)
            {
                if (_clientCount >= _maxClient)
                {
                    //TODO 客户端过多异常
                    RaiseOtherException(null);
                }
                else
                {

                    Socket clientSock = _serverSock.Accept();
                    _clientCount++;
                    IPEndPoint ip = (IPEndPoint)clientSock.RemoteEndPoint;
                    Console.WriteLine("获取到来自{0} : {1}的连接", ip.Address, ip.Port);
                    //TODO 创建一个处理客户端的线程并启动
                    handle = new SocketClientHandle(clientSock);
                    _clients.Add(handle);

                    //使用线程池来操作
                    ThreadPool.QueueUserWorkItem(new WaitCallback(handle.RecevieData));

                    //Thread pthread;
                    //pthread = new Thread(new ThreadStart(client.RecevieData));
                    //pthread.Start();
                    //这里应该使用线程池来进行
                }
            }
        }
Exemplo n.º 21
0
 public SocketEventArgs(string msg, SocketClientHandle handle)
 {
     this._msg    = msg;
     this._handle = handle;
     IsHandled    = false;
 }
Exemplo n.º 22
0
 public SocketEventArgs(SocketClientHandle handle)
 {
     this._handle = handle;
     IsHandled = false;
 }