Beispiel #1
0
 /**
  * Handles a new client connecting to the server and starts reading from the client if the client limit has not been reached.
  *
  * @param newConnection
  *  The new client.
  */
 private void HandleNewClient(Sockets.TcpClient newConnection)
 {
     if(currentClients <= maximumClients || maximumClients == 0) {
         currentClients++;
         if(connectEvent != null) connectEvent(newConnection);
         ConnectionState connectionState = new ConnectionState(newConnection, readLength);
         BeginRead(connectionState);
     }else{
         Out.Logger.WriteOutput("Server full, rejecting client.", Out.Logger.LogLevel.Warn);
         newConnection.Client.Shutdown(Sockets.SocketShutdown.Both);
         newConnection.Client.Disconnect(true);
     }
 }
Beispiel #2
0
 /**
  * Handles data sent by a connection. Recursively reads the received data between delimiters.
  *
  * @param handleConnection
  *  The connection that we have recieved data from.
  */
 private void HandleData(ConnectionState handleConnection)
 {
     int splitPos = handleConnection.BufferString.IndexOf(delimiterString);
     if(handleConnection.Connected & splitPos != -1) {
         string strPacket = handleConnection.BufferString.Substring(0, splitPos);
         if(receiveEvent != null) receiveEvent(handleConnection.ClientConnection, strPacket);
         handleConnection.BufferString = handleConnection.BufferString.Substring(splitPos + delimiterString.Length);
         HandleData(handleConnection);
     }else if(handleConnection.Connected == false) {
         DisconnectHandler(handleConnection);
     }else if(splitPos == -1) {
         BeginRead(handleConnection);
     }
 }
Beispiel #3
0
 /**
  * Handles the disconnection of a client from the server, calls the onDisconnect event.
  *
  * @param disconnectConnection
  *  The client that is disconnecting.
  */
 private void DisconnectHandler(ConnectionState disconnectConnection)
 {
     currentClients--;
         if(disconnectEvent != null) disconnectEvent(disconnectConnection.ClientConnection);
         disconnectConnection.ClientConnection.Client.Disconnect(true);
         disconnectConnection = null;
 }
Beispiel #4
0
 /**
  * End reading from a connected client.
  *
  * @param readConnection
  *  The ConnectionState object of the client to stop reading from.
  * @param asyncResult
  *  The IAsyncResult returned from the asynchronous reading.
  */
 private int EndRead(ConnectionState readConnection, System.IAsyncResult asyncResult)
 {
     try {
         return readConnection.ClientStream.EndRead(asyncResult);
     }catch(System.Exception readEx) {
         Asterion.Out.Logger.WriteOutput("Could not end read: " + readEx.Message + ".", Asterion.Out.Logger.LogLevel.Error);
         return 0;
     }
 }
Beispiel #5
0
 /**
  * Begin reading from a connected client.
  *
  * @param readConnection
  *  The ConnectionState object of the client to read from.
  */
 private void BeginRead(ConnectionState readConnection)
 {
     if(readConnection.ClientStream.CanRead) {
         try {
         readConnection.ClientStream.BeginRead(readConnection.Buffer, 0, readLength, ReceiveCallback, readConnection);
         }catch{
             DisconnectClient(readConnection.ClientConnection);
         }
     }else{
         DisconnectHandler(readConnection);
     }
 }