/// <summary>
        /// Listens for this instance.
        /// </summary>
        private void SwitchListening()
        {
            try
            {
                NetworkStream clientStream = this.Client.GetStream();

                byte[] message;
                int bytesRead;
                WriteStream caller = new WriteStream(this.parentNode.WriteToNetworkStream);

                while (true)
                {
                    bytesRead = 0;
                    message = GlobalConst.getSpaceByteArray(GlobalConst.BUFSIZE);

                    lock (this.toSend)
                    {
                        if (this.toSend.Count < 1)
                        {
                            clientStream.ReadTimeout = 30000;
                        }
                    }

                    try
                    {
                        //blocks until a client sends a message
                        bytesRead = clientStream.Read(message, 0, GlobalConst.BUFSIZE);
                    }
                    catch
                    {
                        //a socket error has occured
                        Console.WriteLine("Socket error on node " + this.parentNode.Name);
                        break;
                    }

                    if (bytesRead == 0)
                    {
                        //the client has disconnected from the server
                        Console.WriteLine("Node " + this.parentNode.Name + " has disconnected from the switch.");
                        break;
                    }

                    SwitchMessage msg = new SwitchMessage();
                    bool msgOk = msg.Decode(message);
                    Console.WriteLine("Got message on switch node " + this.parentNode.Name);

                    if(!msgOk)
                    {

                    }
                    else if (msg.Ack)
                    {
                        if (SenderThread.ThreadState.Equals(ThreadState.WaitSleepJoin))
                        {
                            //Notify the sender thread that the receiving node did get it.
                            SenderThread.Interrupt();
                        }
                        else
                        {
                            //Console.WriteLine("Node {0} sent to node {1} (really {2}) an ack when it wasn't supposed to!", msg.Sender, msg.Destination, this.name);
                        }
                    }
                    else
                    {
                        //message has successfully been received
                        parentNode.writeOutMessage(msg);
                        SwitchMessage ackMsg = new SwitchMessage();
                        ackMsg.EncodeHiddenComm(msg.Sender, msg.Destination);

                        lock (clientStream)
                        {
                            if (clientStream.CanWrite)
                            {
                                caller.BeginInvoke(clientStream, ackMsg, null, null);
                            }
                            else
                            {
                                Console.WriteLine("Node " + this.ParentNode.Name + " can't send an ack.");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            if(!this.doneSending)
            {
                this.senderThread.Abort();
                this.doneSending = true;
            }

            this.doneReceiving = true;

            while(!this.parentThread.ThreadState.Equals(ThreadState.WaitSleepJoin))
            {
                Thread.Sleep(100);
            }
            this.parentThread.Interrupt();
            //this.client.Close();
        }
Esempio n. 2
0
        /// <summary>
        /// Handles the client comm. STarts a new thread for each node.
        /// </summary>
        /// <param name="clientObject">The client object.</param>
        private void HandleClientComm(object clientObject)
        {
            try
            {
                TcpClient client = (TcpClient)clientObject;
                NetworkStream clientStream = client.GetStream();

                byte[] message;
                int bytesRead;

                while (true)
                {
                    bytesRead = 0;
                    message = GlobalConst.getSpaceByteArray(GlobalConst.BUFSIZE);

                    try
                    {
                        //blocks until a client sends a message
                        bytesRead = clientStream.Read(message, 0, GlobalConst.BUFSIZE);

                    }
                    catch
                    {
                        //a socket error has occured
                        break;
                    }

                    if (bytesRead == 0)
                    {
                        //the client has disconnected from the server
                        //this.nodeDisconnectedCount++;
                        break;
                    }

                    SwitchMessage msg = new SwitchMessage();
                    bool msgOk = msg.Decode(message);

                    if(!msgOk)
                    {
                        continue;
                    }

                    checkIfKnown(client, msg.Sender);

                    //message has successfully been received
                    try
                    {
                        if (msg.Destination.Equals(GlobalConst.serverName))
                        {
                            this.nodeDisconnectedCount++;
                        }
                        else
                        {
                            enqueMessage(msg);
                        }
                    }
                    catch
                    {

                        if (msg.Destination == null)
                        {
                            Console.WriteLine("msg.Dest is null.");

                        }
                        else if (GlobalConst.serverName == null)
                        {
                            Console.WriteLine("ServerName is null.");
                        }
                    }
                }
            }
            catch(ThreadInterruptedException)
            {
                TcpClient c = (TcpClient)clientObject;
                c.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }