Example #1
0
 private void StartThread()
 {
     try
     {
         while (this.isRun)
         {
             try
             {
                 byte[] buffer = new byte[0x400];
                 if (this.socket.Receive(buffer, SocketFlags.Peek) != 0)
                 {
                     if (this.OnReceive != null)
                     {
                         ReceiveEventArgs e = new ReceiveEventArgs(this.socket, this.server.Clients);
                         this.OnReceive(this, e);
                     }
                     continue;
                 }
                 if (this.OnDisconnect != null)
                 {
                     this.OnDisconnect(this, new SocketEventArgs(this.socket));
                 }
                 this.server.RemoveClient(this);
                 return;
             }
             catch (Exception)
             {
                 this.isRun = false;
                 if (this.OnDisconnect != null)
                 {
                     this.OnDisconnect(this, new SocketEventArgs(this.socket));
                 }
                 this.server.RemoveClient(this);
                 continue;
             }
         }
     }
     catch (Exception)
     {
     }
     finally
     {
         this.socket.Shutdown(SocketShutdown.Both);
         this.socket.Close();
     }
 }
Example #2
0
 private void StartThread()
 {
     try
     {
         while (this.isRun)
         {
             try
             {
                 byte[] buffer = new byte[0x400];
                 if (this.socket.Receive(buffer, SocketFlags.Peek) != 0)
                 {
                     if (this.OnReceive != null)
                     {
                         ReceiveEventArgs e = new ReceiveEventArgs(this.socket, this.server.Clients);
                         this.OnReceive(this, e);
                     }
                     continue;
                 }
                 if (this.OnDisconnect != null)
                 {
                     this.OnDisconnect(this, new SocketEventArgs(this.socket));
                 }
                 this.server.RemoveClient(this);
                 return;
             }
             catch (Exception)
             {
                 this.isRun = false;
                 if (this.OnDisconnect != null)
                 {
                     this.OnDisconnect(this, new SocketEventArgs(this.socket));
                 }
                 this.server.RemoveClient(this);
                 continue;
             }
         }
     }
     catch (Exception)
     {
     }
     finally
     {
         this.socket.Shutdown(SocketShutdown.Both);
         this.socket.Close();
     }
 }
Example #3
0
 private void ReceiveData()
 {
     while (this.isConnected)
     {
         try
         {
             byte[] buffer = new byte[0x400];
             if (this.client.Receive(buffer, SocketFlags.Peek) != 0)
             {
                 ReceiveEventArgs e = new ReceiveEventArgs(this.client, null);
                 this.OnReceive(this, e);
             }
             else
             {
                 this.isConnected = false;
                 if (this.OnServerClosed != null)
                 {
                     this.OnServerClosed(this, new SocketEventArgs(this.hostAddress, this.port));
                 }
             }
             continue;
         }
         catch (Exception)
         {
             this.isConnected = false;
             if (this.OnServerClosed != null)
             {
                 this.OnServerClosed(this, new SocketEventArgs(this.hostAddress, this.port));
             }
             continue;
         }
     }
     this.Close();
 }
Example #4
0
        private void server_OnReceive(object sender, ReceiveEventArgs e)
        {
            Message message = null;
            if (null != protocol)
                message = protocol.Parse(e.Read());
            else
            {
                message = new Message(e.Read());
            }
            string text = string.Format("recv: <--- {0}", e.Read());
            WriteToLog(text);

            if (message.Parsed)
                DispatchState(message.Command, message.Parameters);
            dtTime = DateTime.Now;
        }