예제 #1
0
        private bool CreateInstance(TcpClient tcp)
        {
            try
            {
                IPEndPoint ipEndPoint = tcp.Client.RemoteEndPoint as IPEndPoint;
                if (ipEndPoint == null)
                {
                    return(false);
                }
                string clientIp = ipEndPoint.ToString();
                if (ClientInstance.ContainsKey(clientIp))
                {
                    ClientInstance.Remove(clientIp);
                }
                // 建立玩家peer實體
                ClientNode cNode = _SocketServer.GetPeer(this, ipEndPoint, _SocketServer);
                //註冊到 mListener 中,讓他的 Receive 功能能被叫
                TCPInstance instance = new TCPInstance(this, cNode, tcp);
                //註冊到 mListener 中,讓他的 Receive 功能能被叫
                ClientInstance.Add(clientIp, instance);
                //成功加入後傳送 Connect 事件給 Client
                byte[] packet = new byte[] { 1 };
                tcp.GetStream().Write(packet, 0, packet.Length);
                cNode.Initialize();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(false);
            }

            return(true);
        }
예제 #2
0
 public override void DisConnect(IPEndPoint iPEndPoint)
 {
     lock (ClientInstance)
     {
         string key = iPEndPoint.ToString();
         if (ClientInstance.TryGetValue(key, out Instance instance))
         {
             TCPInstance tcpInstance = (TCPInstance)instance;
             tcpInstance.Dispose();
             ClientInstance.Remove(key);
         }
     }
 }
예제 #3
0
        private async Task StartReceiveAsync(TcpClient tcp)
        {
            IPEndPoint ipEndPoint = tcp.Client.RemoteEndPoint as IPEndPoint;

            if (ClientInstance.TryGetValue(ipEndPoint.ToString(), out Instance instance))
            {
                TCPInstance tcpInstance = (TCPInstance)instance;

                NetworkStream stream = tcp.GetStream();
                await Task.Run(async() =>
                {
                    try
                    {
                        while (tcp.Client.Connected)
                        {
                            byte[] buff = new byte[BufferSize];
                            int count   = await stream.ReadAsync(buff, 0, buff.Length);
                            Array.Resize(ref buff, count);
                            if (count.Equals(0))
                            {
                                DisConnect(ipEndPoint);
                                break;
                            }
                            // 將接收到的buff data送入client佇列等候處理
                            tcpInstance.PassData(buff);
                        }
                    }
                    catch (IOException e)
                    {
                        if (!e.Message.Contains(ErrorMsg1))
                        {
                            Console.WriteLine(e.Message + "," + e.HResult);
                        }
                        else
                        {
                            DisConnect(ipEndPoint);
                        }
                    }
                });
            }
        }