예제 #1
0
        // Private Methods
        //********************************************************************
        /// <summary> Called when a message arrives </summary>
        /// <param name="ar"> RefType: An async result interface </param>
        private void ReceiveComplete(IAsyncResult ar)
        {
            try {
                // Is the Network Stream object valid
                if (GetNetworkStream.CanRead)
                {
                    // Read the current bytes from the stream buffer
                    int iBytesRecieved = GetNetworkStream.EndRead(ar);

                    // If there are bytes to process else the connection is lost
                    if (iBytesRecieved > 0)
                    {
                        // A message came in send it to the MessageHandler
                        try { GetMessageHandler(this, iBytesRecieved); }
                        catch (Exception ex) { LogLib.WriteLine("Error with GetMessageHandler() in CSocketClient.ReceiveComplete(): ", ex); }

                        // Wait for a new message
                        Receive();
                    }
                    else
                    {
                        LogLib.WriteLine("CSocketClient.ReceiveComplete(): Shuting Down", LogLevel.Error);
                        throw new Exception("Shut Down");
                    }
                }
            }
            catch (Exception) {
                // The connection must have dropped call the CloseHandler
                try { GetCloseHandler(this); }
                catch (Exception ex) { LogLib.WriteLine("Error in CSocketClient.ReceiveComplete(): ", ex); }

                // Dispose of the class
                Dispose();
            }
        }
예제 #2
0
        //********************************************************************
        /// <summary> Function used to disconnect from the server </summary>
        public void Disconnect()
        {
            LogLib.WriteLine("Entering in CSocketClient.Disconnect()", LogLevel.Trace);

            // Close down the connection
            if (GetNetworkStream != null)
            {
                GetNetworkStream.Close();
            }

            if (GetTcpClient != null)
            {
                GetTcpClient.Close();
            }

            if (GetClientSocket != null)
            {
                GetClientSocket.Close();
            }

            // Clean up the connection state
            GetClientSocket  = null;
            GetNetworkStream = null;
            GetTcpClient     = null;

            LogLib.WriteLine("Exiting in CSocketClient.Disconnect()", LogLevel.Trace);
        }
예제 #3
0
 //********************************************************************
 /// <summary> Wait for a message to arrive </summary>
 public void Receive()
 {
     if (GetNetworkStream?.CanRead == true)
     {
         // Issue an asynchronous read
         GetNetworkStream.BeginRead(GetRawBuffer, 0, GetSizeOfRawBuffer, GetCallbackReadMethod, null);
     }
     else
     {
         LogLib.WriteLine("Error in CSocketClient.Receive(): Socket Closed");
     }
 }
예제 #4
0
 /// <summary> Function to send a raw buffer to the server </summary>
 /// <param name="pRawBuffer"> RefType: A Raw buffer of bytes to send </param>
 public void Send(byte[] pRawBuffer)
 {
     if (GetNetworkStream?.CanWrite == true)
     {
         // Issue an asynchronus write
         GetNetworkStream.BeginWrite(pRawBuffer, 0, pRawBuffer.GetLength(0), GetCallbackWriteMethod, null);
     }
     else
     {
         LogLib.WriteLine("Error in CSocketClient.Send(Byte[]): Socket Closed");
     }
 }
예제 #5
0
 //********************************************************************
 /// <summary> Called when a message is sent </summary>
 /// <param name="ar"> RefType: An async result interface </param>
 private void SendComplete(IAsyncResult ar)
 {
     try {
         // Is the Network Stream object valid
         if (GetNetworkStream.CanWrite)
         {
             GetNetworkStream.EndWrite(ar);
             LogLib.WriteLine("CSocketClient.SendComplete(): GetNetworkStream.EndWrite()", LogLevel.Debug);
         }
     }
     catch (Exception ex) { LogLib.WriteLine("Error in CSocketClient.SendComplete(): ", ex); }
 }
예제 #6
0
        //********************************************************************
        /// <summary> Function used to disconnect from the server </summary>
        public void Disconnect()
        {
            // Close down the connection
            GetNetworkStream?.Close();

            GetTcpClient?.Close();

            GetClientSocket?.Close();

            // Clean up the connection state
            GetClientSocket  = null;
            GetNetworkStream = null;
            GetTcpClient     = null;
        }
예제 #7
0
        //********************************************************************
        /// <summary> Wait for a message to arrive </summary>
        public void Receive()
        {
            LogLib.WriteLine("Entering in CSocketClient.Receive()", LogLevel.Trace);
            if ((GetNetworkStream != null) && (GetNetworkStream.CanRead))
            {
                // Issue an asynchronous read
                GetNetworkStream.BeginRead(GetRawBuffer, 0, GetSizeOfRawBuffer, GetCallbackReadMethod, null);
            }
            else
            {
                LogLib.WriteLine("Error in CSocketClient.Receive(): Socket Closed");
            }

            LogLib.WriteLine("Exiting in CSocketClient.Receive()", LogLevel.Trace);
        }
예제 #8
0
        //********************************************************************
        /// <summary> Function to send a raw buffer to the server </summary>
        /// <param name="pRawBuffer"> RefType: A Raw buffer of bytes to send </param>
        public void Send(Byte[] pRawBuffer)
        {
            LogLib.WriteLine("Entering in CSocketClient.Send(Byte[])", LogLevel.Trace);
            if ((GetNetworkStream != null) && (GetNetworkStream.CanWrite))
            {
                // Issue an asynchronus write
                GetNetworkStream.BeginWrite(pRawBuffer, 0, pRawBuffer.GetLength(0), GetCallbackWriteMethod, null);
            }
            else
            {
                LogLib.WriteLine("Error in CSocketClient.Send(Byte[]): Socket Closed");
            }

            LogLib.WriteLine("Exiting in CSocketClient.Send(Byte[])", LogLevel.Trace);
        }
예제 #9
0
        //********************************************************************
        /// <summary> Function to send a string to the server </summary>
        /// <param name="strMessage"> RefType: A string to send </param>
        public void Send(String strMessage)
        {
            LogLib.WriteLine("Entering in CSocketClient.Send(String): " + strMessage, LogLevel.Trace);

            if ((GetNetworkStream != null) && (GetNetworkStream.CanWrite))
            {
                // Convert the string into a Raw Buffer
                Byte[] pRawBuffer = System.Text.Encoding.ASCII.GetBytes(strMessage);

                // Issue an asynchronus write
                GetNetworkStream.BeginWrite(pRawBuffer, 0, pRawBuffer.GetLength(0), GetCallbackWriteMethod, null);
            }
            else
            {
                LogLib.WriteLine("Error in CSocketClient.Send(string): Socket Closed");
            }

            LogLib.WriteLine("Exiting in CSocketClient.Send(String): " + strMessage, LogLevel.Trace);
        }
예제 #10
0
 //********************************************************************
 /// <summary> Function used to disconnect from the server </summary>
 public void Disconnect()
 {
     // Close down the connection
     GetNetworkStream?.Close();
     GetTcpClient?.Close();
 }