예제 #1
0
        /// <summary>
        /// processes caught message. starts the client running
        /// to send the processed message onto the endpoint server.
        /// </summary>
        /// <param name="serverState">
        /// ServerStateSaver object
        /// </param>
        private void ProcessMessage(ServerStateSaver serverState)
        {
            // save message to log builder dictionary
            lock (serverState.serverMessageAdd)
            {
                serverState.lb.messageLogList.Add(serverState.serverMessage);
            }

            // create instance of the ResponseBuilder
            ResponseBuilder rb = new ResponseBuilder(serverState);

            rb.HostResponse();

            serverState.localClient.BeginTransmission();
        }
        /// <summary>
        /// Returns the endpoint message back to the client
        /// </summary>
        /// <param name="serverState">
        /// server state saver class object
        /// </param>
        public void ReturnMessage(ServerStateSaver serverState)
        {
            try
            {
                // Add message to the log
                lock (serverState.serverMessageAdd)
                {
                    serverState.lb.messageLogList.Add(serverState.returnMsg);
                }

                lock (sendLock)
                {
                    serverState.serverSocket.Send(serverState.returnMsg);
                }
            }
            catch (Exception e)
            {
                e.Message.ToString();
            }
        }
        /// <summary>
        /// Returns the endpoint message back to the client
        /// </summary>
        /// <param name="serverState">
        /// server state saver class object
        /// </param>
        public void ReturnMessage(ServerStateSaver serverState)
        {
            try
            {
                // Add message to the log
                lock (serverState.serverMessageAdd)
                {
                    serverState.lb.messageLogList.Add(serverState.returnMsg);
                }

                lock (sendLock)
                {
                    serverState.serverSocket.Send(serverState.returnMsg);
                }
            }
            catch (Exception e)
            {
                e.Message.ToString();
            }
        }
        /// <summary>
        /// Handles the connection to the endpoint server
        /// sends messages from middlware to endpoint.
        /// </summary>
        /// <param name="clientState">
        /// CleintStateSaver object.
        /// </param>
        private void ThreadSendFunction(ServerStateSaver serverState)
        {
            try
            {
                // sleep the thread based on pace given in gui
                Thread.Sleep(serverState.clientState.pace);

                // lock socket and send the message to endpoint server
                lock (clientSendLock)
                {
                    serverState.clientState.clientSocket.Send(serverState.sendMsg);
                }

                // put the message into the log builder
                lock (serverState.serverMessageAdd)
                {
                    serverState.lb.messageLogList.Add(serverState.sendMsg);
                }
            }
            catch (Exception)
            {
            }
        }
예제 #5
0
        //public Dictionary<int, byte[]> hostForwardMessage = new Dictionary<int, byte[]>();
        //public Dictionary<int, byte[]> clientResponseMessage = new Dictionary<int, byte[]>();

        /// <summary>
        /// Writes the log file
        /// </summary>
        /// <param name="serverState"></param>
        public void WriteLogs(ServerStateSaver serverState)
        {
            //StreamWriter sw = File.AppendText(PATH);
            System.IO.StreamWriter sw = System.IO.File.AppendText(PATH);

            string previousMsg = string.Empty;

            lock (serverState.serverMessageAdd)
            {
                foreach (byte[] item in messageLogList)
                {
                    //string msg = System.Text.Encoding.ASCII.GetString(item);
                    string msg = System.Text.Encoding.ASCII.GetString(item) + "\r\n";

                    if (!previousMsg.Contains(msg))
                    {
                        sw.Write(msg);
                        previousMsg += msg;
                    }
                }
            }

            sw.Close();
        }
예제 #6
0
        //public Dictionary<int, byte[]> hostForwardMessage = new Dictionary<int, byte[]>();
        //public Dictionary<int, byte[]> clientResponseMessage = new Dictionary<int, byte[]>();
        /// <summary>
        /// Writes the log file 
        /// </summary>
        /// <param name="serverState"></param>
        public void WriteLogs(ServerStateSaver serverState)
        {
            //StreamWriter sw = File.AppendText(PATH);
            System.IO.StreamWriter sw = System.IO.File.AppendText(PATH);

            string previousMsg = string.Empty;

            lock (serverState.serverMessageAdd)
            {
                foreach (byte[] item in messageLogList)
                {
                    //string msg = System.Text.Encoding.ASCII.GetString(item);
                    string msg = System.Text.Encoding.ASCII.GetString(item) + "\r\n";

                    if (!previousMsg.Contains(msg))
                    {
                        sw.Write(msg);
                        previousMsg += msg;
                    }
                }
            }

            sw.Close();
        }
        /// <summary>
        /// Handles the connection to the endpoint server
        /// sends messages from middlware to endpoint.
        /// </summary>
        /// <param name="clientState">
        /// CleintStateSaver object.
        /// </param>
        private void ThreadSendFunction(ServerStateSaver serverState)
        {
            try
            {
                // sleep the thread based on pace given in gui
                Thread.Sleep(serverState.clientState.pace);

                // lock socket and send the message to endpoint server
                lock (clientSendLock)
                {
                    serverState.clientState.clientSocket.Send(serverState.sendMsg);
                }

                // put the message into the log builder
                lock (serverState.serverMessageAdd)
                {
                    serverState.lb.messageLogList.Add(serverState.sendMsg);
                }
            }
            catch (Exception)
            {
            }
        }
        /// <summary>
        /// Receiving side connection handler
        /// </summary>
        /// <param name="clientState">
        /// takes the ClientStateSaver being used by the hostconnection
        /// server to keep the messages compiled in the same location
        /// </param>
        private void ReceiveHandler(ServerStateSaver serverState)
        {
            int receivedBytes = 0;
            byte[] byteSize = new byte[LENGTH_BITS];
            byte[] clientBuffer = serverState.clientState.clientBuffer;

            try
            {
                do
                {
                    //int offset = 0;
                    //int size = 0;

                    //receivedBytes = serverState.clientState.clientSocket.Receive(clientBuffer,
                    //    offset, LENGTH_BITS, SocketFlags.None);

                    //Array.Copy(clientBuffer, offset, byteSize, 0, LENGTH_BITS);

                    //if (BitConverter.IsLittleEndian)
                    //{
                    //    Array.Reverse(byteSize);
                    //}

                    //size = BitConverter.ToInt16(byteSize, 0);

                    //offset += LENGTH_BITS;

                    //receivedBytes = serverState.clientState.clientSocket.Receive(clientBuffer,
                    //    offset, size, SocketFlags.None);

                    //serverState.returnMsg = new byte[size];

                    //Array.Copy(clientBuffer, offset, serverState.returnMsg, 0, size);

                    //ReturnMessageClass rmc = new ReturnMessageClass();

                    //Thread returnThread = new Thread(delegate()
                    //    {
                    //        rmc.ReturnMessage(serverState);
                    //    });
                    //returnThread.Start();

                    receivedBytes =
                        serverState.clientState.clientSocket.Receive
                        (serverState.clientState.clientBuffer);

                    serverState.returnMsg = new byte[receivedBytes];

                    Array.Copy(serverState.clientState.clientBuffer,
                        serverState.returnMsg, receivedBytes);

                    serverState.clientState.clientMsgCount++;

                    ReturnMessageClass rmc = new ReturnMessageClass();

                    Thread returnThread = new Thread(delegate()
                        {
                            rmc.ReturnMessage(serverState);
                        });

                    returnThread.Start();
                }
                while (receivedBytes > 0);
            }
            catch (Exception)
            {

            }
        }
예제 #9
0
        /// <summary>
        /// processes caught message. starts the client running
        /// to send the processed message onto the endpoint server.
        /// </summary>
        /// <param name="serverState">
        /// ServerStateSaver object
        /// </param>
        private void ProcessMessage(ServerStateSaver serverState)
        {
            // save message to log builder dictionary
            lock (serverState.serverMessageAdd)
            {
                serverState.lb.messageLogList.Add(serverState.serverMessage);
            }

            // create instance of the ResponseBuilder
            ResponseBuilder rb = new ResponseBuilder(serverState);
            rb.HostResponse();

            serverState.localClient.BeginTransmission();
        }
예제 #10
0
        /// <summary>
        /// Handles the connection to the client
        /// </summary>
        /// <param name="serverState">
        /// ServerStateSaver object to access
        /// current properties dynamically.
        /// </param>
        private void ConnectionHandler(ServerStateSaver serverState)
        {
            // initialize buffer array
            byte[] buffer = new byte[BUFFER_SIZE];

            // message length buffer array
            byte[] byteSize = new byte[LENGTH_BITS];

            // bytes received last read
            int bytesRead = 0;

            // message counter
            int messageCount = 0;

            try
            {
                //while (true)
                //while (serverState.clientState.clientSocket.Connected)
                while (serverState.serverSocket.Connected)
                {
                    // message array pointers
                    int offSet = 0;
                    int size = 0;

                    lock (serverState.receiveLock)
                    {
                        bytesRead = serverState.serverSocket.Receive(buffer,
                            offSet, LENGTH_BITS, SocketFlags.None);
                    }

                    // Get the size values out of current message
                    Array.Copy(buffer, offSet, byteSize, 0, LENGTH_BITS);

                    // Reverse the bits if they aren't in proper order for proc
                    if (BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(byteSize);
                    }

                    // Set the size variable
                    size = BitConverter.ToInt16(byteSize, 0);

                    // Set offSet variable
                    offSet += LENGTH_BITS;

                    lock (serverState.messageLock)
                    {
                        // read next message out of buffer
                        bytesRead = serverState.serverSocket.Receive(buffer, offSet, size,
                            SocketFlags.None);
                    }

                    // Set messageBuffer to correct size
                    serverState.serverMessage = new byte[size];

                    // Copy message into the messageBuffer
                    Array.Copy(buffer, offSet, serverState.serverMessage, 0, size);

                    // Send the message off to be processed
                    //Thread processMessageThread = new Thread(delegate()
                    serverState.serverThread = new Thread(delegate()
                        {
                            ProcessMessage(serverState);
                        });
                    serverState.serverThread.Start();

                    // increment the message counter
                    messageCount++;
                }
            }
            catch (Exception)
            {
            }

            serverState.serverThread.Join();
            serverState.clientState.clientThread.Join();
            serverState.lb.WriteLogs(serverState);
            serverState.serverSocket.Shutdown(SocketShutdown.Both);

            //testDataTextbox.Text +=
            //    "\r\nClient has shut down it's socket, " +
            //    "killing server connection as well." +
            //    "Writing Logs";
        }
예제 #11
0
        /// <summary>
        /// Accepts connections from clients
        /// </summary>
        private void AcceptConnections()
        {
            while (true)
            {
                Socket socket = null;
                int clientCount = 0;

                // Accept connection from client
                try
                {
                    socket = sock.Accept();
                }
                catch (Exception)
                {
                    //testDataTextbox.Text += "ERROR! " + e.Message;
                }

                // Set up the server state saver
                ServerStateSaver serverState = new ServerStateSaver();
                serverState.localServerIP = localServerIP;
                serverState.serverSocket = socket;
                serverState.serverStopWatch.Start();

                // Set up the local client connection
                //clientEndPoint1 = new ClientConnection();
                //clientEndPoint1.ServerState = serverState;
                serverState.localClient = new ClientConnection(serverState);

                // if second client
                if (bonus && clientCount == 1)
                {
                    serverState.localClient.EndPoint = endPoint2;
                }
                else
                {
                    serverState.localClient.EndPoint = endPoint1;
                    //testDataTextbox.Text += "\r\nEndPoint set, start next client";
                }

                // set up the client for endpoint comm
                //serverState.localClient.ServerState = serverState;
                //serverState.localClient = new ClientConnection(serverState);
                //serverState.localClient.testDataTextbox = testDataTextbox;
                serverState.clientState = new ClientStateSaver();
                serverState.clientState.pace = pace;

                // Connect to the endpoint server
                serverState.localClient.Start();

                // Set up and start thread on connection handler function
                serverState.serverThread = new Thread(delegate()
                    {
                        ConnectionHandler(serverState);
                    });

                serverState.serverThread.Start();

                clientCount++;
            }
        }
예제 #12
0
        /// <summary>
        /// Handles the connection to the client
        /// </summary>
        /// <param name="serverState">
        /// ServerStateSaver object to access
        /// current properties dynamically.
        /// </param>
        private void ConnectionHandler(ServerStateSaver serverState)
        {
            // initialize buffer array
            byte[] buffer = new byte[BUFFER_SIZE];

            // message length buffer array
            byte[] byteSize = new byte[LENGTH_BITS];

            // bytes received last read
            int bytesRead = 0;

            // message counter
            int messageCount = 0;

            try
            {
                //while (true)
                //while (serverState.clientState.clientSocket.Connected)
                while (serverState.serverSocket.Connected)
                {
                    // message array pointers
                    int offSet = 0;
                    int size   = 0;

                    lock (serverState.receiveLock)
                    {
                        bytesRead = serverState.serverSocket.Receive(buffer,
                                                                     offSet, LENGTH_BITS, SocketFlags.None);
                    }

                    // Get the size values out of current message
                    Array.Copy(buffer, offSet, byteSize, 0, LENGTH_BITS);

                    // Reverse the bits if they aren't in proper order for proc
                    if (BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(byteSize);
                    }

                    // Set the size variable
                    size = BitConverter.ToInt16(byteSize, 0);

                    // Set offSet variable
                    offSet += LENGTH_BITS;

                    lock (serverState.messageLock)
                    {
                        // read next message out of buffer
                        bytesRead = serverState.serverSocket.Receive(buffer, offSet, size,
                                                                     SocketFlags.None);
                    }

                    // Set messageBuffer to correct size
                    serverState.serverMessage = new byte[size];

                    // Copy message into the messageBuffer
                    Array.Copy(buffer, offSet, serverState.serverMessage, 0, size);

                    // Send the message off to be processed
                    //Thread processMessageThread = new Thread(delegate()
                    serverState.serverThread = new Thread(delegate()
                    {
                        ProcessMessage(serverState);
                    });
                    serverState.serverThread.Start();

                    // increment the message counter
                    messageCount++;
                }
            }
            catch (Exception)
            {
            }

            serverState.serverThread.Join();
            serverState.clientState.clientThread.Join();
            serverState.lb.WriteLogs(serverState);
            serverState.serverSocket.Shutdown(SocketShutdown.Both);

            //testDataTextbox.Text +=
            //    "\r\nClient has shut down it's socket, " +
            //    "killing server connection as well." +
            //    "Writing Logs";
        }
 public ClientConnection(ServerStateSaver serverState)
 {
     this.serverState = serverState;
 }
        /// <summary>
        /// Receiving side connection handler
        /// </summary>
        /// <param name="clientState">
        /// takes the ClientStateSaver being used by the hostconnection
        /// server to keep the messages compiled in the same location
        /// </param>
        private void ReceiveHandler(ServerStateSaver serverState)
        {
            int receivedBytes = 0;

            byte[] byteSize     = new byte[LENGTH_BITS];
            byte[] clientBuffer = serverState.clientState.clientBuffer;

            try
            {
                do
                {
                    //int offset = 0;
                    //int size = 0;

                    //receivedBytes = serverState.clientState.clientSocket.Receive(clientBuffer,
                    //    offset, LENGTH_BITS, SocketFlags.None);

                    //Array.Copy(clientBuffer, offset, byteSize, 0, LENGTH_BITS);

                    //if (BitConverter.IsLittleEndian)
                    //{
                    //    Array.Reverse(byteSize);
                    //}

                    //size = BitConverter.ToInt16(byteSize, 0);

                    //offset += LENGTH_BITS;

                    //receivedBytes = serverState.clientState.clientSocket.Receive(clientBuffer,
                    //    offset, size, SocketFlags.None);

                    //serverState.returnMsg = new byte[size];

                    //Array.Copy(clientBuffer, offset, serverState.returnMsg, 0, size);

                    //ReturnMessageClass rmc = new ReturnMessageClass();

                    //Thread returnThread = new Thread(delegate()
                    //    {
                    //        rmc.ReturnMessage(serverState);
                    //    });
                    //returnThread.Start();

                    receivedBytes =
                        serverState.clientState.clientSocket.Receive
                            (serverState.clientState.clientBuffer);

                    serverState.returnMsg = new byte[receivedBytes];

                    Array.Copy(serverState.clientState.clientBuffer,
                               serverState.returnMsg, receivedBytes);

                    serverState.clientState.clientMsgCount++;

                    ReturnMessageClass rmc = new ReturnMessageClass();

                    Thread returnThread = new Thread(delegate()
                    {
                        rmc.ReturnMessage(serverState);
                    });

                    returnThread.Start();
                }while (receivedBytes > 0);
            }
            catch (Exception)
            {
            }
        }
예제 #15
0
 /// <summary>
 /// Non-default constructor, takes a ServerStateSaver
 /// object so response can be created to be passed to
 /// endpoint server
 /// </summary>
 /// <param name="serverState">
 /// ServerStateSaver object with message
 /// </param>
 public ResponseBuilder(ServerStateSaver serverState)
 {
     this.serverState = serverState;
 }
 public ClientConnection(ServerStateSaver serverState)
 {
     this.serverState = serverState;
 }
예제 #17
0
 /// <summary>
 /// Non-default constructor, takes a ServerStateSaver
 /// object so response can be created to be passed to
 /// endpoint server
 /// </summary>
 /// <param name="serverState">
 /// ServerStateSaver object with message
 /// </param>
 public ResponseBuilder(ServerStateSaver serverState)
 {
     this.serverState = serverState;
 }
예제 #18
0
        /// <summary>
        /// Accepts connections from clients
        /// </summary>
        private void AcceptConnections()
        {
            while (true)
            {
                Socket socket      = null;
                int    clientCount = 0;

                // Accept connection from client
                try
                {
                    socket = sock.Accept();
                }
                catch (Exception)
                {
                    //testDataTextbox.Text += "ERROR! " + e.Message;
                }

                // Set up the server state saver
                ServerStateSaver serverState = new ServerStateSaver();
                serverState.localServerIP = localServerIP;
                serverState.serverSocket  = socket;
                serverState.serverStopWatch.Start();

                // Set up the local client connection
                //clientEndPoint1 = new ClientConnection();
                //clientEndPoint1.ServerState = serverState;
                serverState.localClient = new ClientConnection(serverState);

                // if second client
                if (bonus && clientCount == 1)
                {
                    serverState.localClient.EndPoint = endPoint2;
                }
                else
                {
                    serverState.localClient.EndPoint = endPoint1;
                    //testDataTextbox.Text += "\r\nEndPoint set, start next client";
                }

                // set up the client for endpoint comm
                //serverState.localClient.ServerState = serverState;
                //serverState.localClient = new ClientConnection(serverState);
                //serverState.localClient.testDataTextbox = testDataTextbox;
                serverState.clientState      = new ClientStateSaver();
                serverState.clientState.pace = pace;


                // Connect to the endpoint server
                serverState.localClient.Start();

                // Set up and start thread on connection handler function
                serverState.serverThread = new Thread(delegate()
                {
                    ConnectionHandler(serverState);
                });

                serverState.serverThread.Start();

                clientCount++;
            }
        }