Ejemplo n.º 1
0
        /// <summary>
        /// 处理连接请求
        /// </summary>
        /// <param name="e"></param>
        private void ProcessAccept(SocketAsyncEventArgs e)
        {
            //限制线程的访问
            acceptSemaphore.WaitOne();
            //获取客户端
            ClientPeer clientPeer = clientPeerPool.Dequeue();

            clientPeer.ClientSocket = e.AcceptSocket;

            Console.WriteLine("客户端连接成功 :" + clientPeer.ClientSocket.RemoteEndPoint.ToString());
            //
            //application.onConnect(clientPeer);
            //开始接收数据
            StartReceive(clientPeer);
            //清空
            e.AcceptSocket = null;
            //
            StartAccept(e);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 开启服务器
        /// </summary>
        /// <param name="port">端口号</param>
        /// <param name="maxCount">最大连接数</param>
        public void Start(int port, int maxCount)
        {
            try
            {
                //创建socket
                serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                //限制访问线程数
                acceptSemaphore = new Semaphore(maxCount, maxCount);

                //初始化连接池
                clientPeerPool = new ClientPeerPool(maxCount);
                ClientPeer tmpClientPeer;
                for (int i = 0; i < maxCount; i++)
                {
                    tmpClientPeer = new ClientPeer();

                    tmpClientPeer.receiveCompleted      += ReceiveCompleted;
                    tmpClientPeer.receiveArgs.Completed += Receive_Completed;
                    tmpClientPeer.sendDisconnect        += DisConnect;

                    clientPeerPool.Enqueue(tmpClientPeer);
                }

                //绑定ip端口号
                serverSocket.Bind(new IPEndPoint(IPAddress.Any, port));
                //连接数量
                serverSocket.Listen(10);

                Console.WriteLine("服务器启动...");

                StartAccept(null);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 断开连接
        /// </summary>
        /// <param name="client"></param>
        /// <param name="reason">断开的原因</param>
        public void DisConnect(ClientPeer client, string reason)
        {
            try
            {
                if (client == null)
                {
                    throw new Exception("当前指定的客户端不存在,无法断开连接");
                }
                Console.WriteLine("客户端:" + client.ClientSocket.RemoteEndPoint.ToString() + "断开连接,原因:" + reason);

                //通知应用层 这个客户端断开连接了
                application.OnDisconnect(client);

                client.Disconnect();
                //回收当前客户端
                clientPeerPool.Enqueue(client);
                //退出信号量并返回上一个计数。
                acceptSemaphore.Release();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
Ejemplo n.º 4
0
        public void OnRecive(ClientPeer client, SocketMessage msg)
        {
            switch (msg.OpCode)
            {
            case OpCode.ACCOUNT:
                account.OnRecive(client, msg.SubCode, msg.Value);
                break;

            case OpCode.USER:
                user.OnRecive(client, msg.SubCode, msg.Value);
                break;

            case OpCode.MATCH:
                match.OnRecive(client, msg.SubCode, msg.Value);
                break;

            case OpCode.FIGHT:
                fight.OnRecive(client, msg.SubCode, msg.Value);
                break;

            default:
                break;
            }
        }
Ejemplo n.º 5
0
 /// <summary>
 /// 数据解析完成后的处理
 /// </summary>
 /// <param name="client"></param>
 /// <param name="value"></param>
 private void ReceiveCompleted(ClientPeer client, SocketMsg value)
 {
     application.OnReceive(client, value);
 }
Ejemplo n.º 6
0
 /// <summary>
 /// 添加队列
 /// </summary>
 public void Enqueue(ClientPeer client)
 {
     clientPeersQueue.Enqueue(client);
 }
Ejemplo n.º 7
0
 public void OnDisconnect(ClientPeer client)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 8
0
 /// <summary>
 /// 有新的客户端加入时调用
 /// </summary>
 /// <param name="client"></param>
 public void OnConnect(ClientPeer client)
 {
 }