예제 #1
0
 protected void ThreadedAccept()
 {
     while (this.active)
     {
         Thread.Sleep(this.TickRate);
         TcpClient     tcpClient     = null;
         NetworkStream networkStream = null;
         try
         {
             tcpClient     = this.tcp.AcceptTcpClient();
             networkStream = tcpClient.GetStream();
         }
         catch (SocketException ex)
         {
             if (this.OnError != null)
             {
                 this.OnError(this, new NetExceptionEventArgs(ex));
             }
             if (networkStream != null)
             {
                 networkStream.Close();
             }
             if (tcpClient != null)
             {
                 tcpClient.Close();
             }
             continue;
         }
         NetBaseStream <T> netBaseStream = this.CreateStream(networkStream, tcpClient.Client.RemoteEndPoint);
         netBaseStream.OnStopped  += new NetStreamStoppedEventHandler(this.OnClientStopped);
         netBaseStream.OnReceived += new NetStreamReceivedEventHandler <T>(this.OnClientReceived);
         netBaseStream.Start();
         this.clients.Add(netBaseStream.Guid);
         this.streams.Add(netBaseStream.Guid, netBaseStream);
         NetClientConnectedEventArgs netClientConnectedEventArgs = new NetClientConnectedEventArgs(netBaseStream.Guid, false);
         if (this.OnClientConnected != null)
         {
             this.OnClientConnected(this, netClientConnectedEventArgs);
         }
         if ((this.ClientCount < this.MaxClients || this.MaxClients == 0) && !netClientConnectedEventArgs.Reject)
         {
             if (this.OnClientAccepted != null)
             {
                 this.OnClientAccepted(this, new NetClientAcceptedEventArgs(netBaseStream.Guid));
             }
         }
         else
         {
             if (this.OnClientRejected != null)
             {
                 this.OnClientRejected(this, new NetClientRejectedEventArgs(netBaseStream.Guid, NetRejectedReason.Other));
             }
             networkStream.Close();
             tcpClient.Close();
         }
     }
     this.Stop(NetStoppedReason.Manually);
 }
예제 #2
0
        public void Connect(string host, int port)
        {
            if (this.IsConnected)
            {
                this.Disconnect(NetStoppedReason.Manually);
            }
            this.RemoteHost = host;
            this.RemotePort = port;
            this.tcp        = new TcpClient();
            this.tcp.Connect(host, port);
            this.IsConnected = true;
            if (this.OnConnected != null)
            {
                this.OnConnected(this, new NetConnectedEventArgs());
            }
            NetworkStream ns             = this.tcp.GetStream();
            EndPoint      remoteEndPoint = this.tcp.Client.RemoteEndPoint;

            this.stream             = this.CreateStream(ns, remoteEndPoint);
            this.stream.OnReceived += new NetStreamReceivedEventHandler <T>(this.stream_OnReceived);
            this.stream.OnStopped  += new NetStreamStoppedEventHandler(this.stream_OnStopped);
            this.stream.Start();
        }