示例#1
0
        private void SocketHandler()
        {

            NetMessage m = new NetMessage();
            SimulationEvent e = null;
            while (true)
            {
                try
                {
                    m.Receive(ref netStream);
                    switch (m.type)
                    {
                        case NetMessageType.EVENT:
                            try
                            {
                                e = SimulationEventFactory.XMLDeserialize(m.msg);
                                lock (eventQueueLock)
                                {
                                    eventQueue.Add(e);
                                }
                            }
                            catch (Exception exc)
                            {
                                ErrorLog.Write(String.Format("NONFATAL Deserialize Error in NetworkClient: {0}", m.msg));
                                ErrorLog.Write(exc.ToString());
                            }
                            break;
                        case NetMessageType.DISCONNECT:
                            System.Console.WriteLine("NetworkClient: recieved Shutdown message from server.  Disconnecting...");
                            Disconnect();
                            return;
                        case NetMessageType.PING:
                            break;
                        case NetMessageType.NONE:
                            
                            System.Console.WriteLine("NetworkClient: Message with no type received.  Disconnecting.");
                            Disconnect();
                            return;
                        default:
                            throw new Exception("NetworkClient: recieved unhandled message");
                    }
                }
                catch (System.IO.IOException)
                {
                    System.Console.WriteLine("NetworkClient: Lost connection with remote server!");
                    netStream.Close();
                    netStream.Dispose();
                    isConnected = false;
                    clientThread.Abort();
                    return;
                }
            }
        }
示例#2
0
        private void ConnectionHandler()
        {
            NetMessage m = new NetMessage();
            SimulationEvent e = null;
            while (true)
            {
                try
                {
                    m.Receive(ref netStream);
                    switch (m.type)
                    {
                        case NetMessageType.REGISTER:
                            eventDistClient = new SimulationEventDistributorClient();
                            networkServer.eventDist.RegisterClient(ref eventDistClient);
                            m_terminalID = m.TerminalID;
                            m.type = NetMessageType.REGISTER_RESPONSE;
                            //m.clientID = networkServer.RegisterClient();
                            m.clientID = eventDistClient.id;
                            m.Send(ref netStream, m.TerminalID);
                            break;
                        case NetMessageType.SUBSCRIBE:
                            eventDistClient.Subscribe(m.msg);
                            break;
                        case NetMessageType.EVENT:
                            try
                            {
                                e = SimulationEventFactory.XMLDeserialize(m.msg);
                                eventDistClient.PutEvent(e);
                            }
                            catch (Exception exc)
                            {
                                ErrorLog.Write(String.Format("NONFATAL Deserialize Error in NetworkServer: {0}", m.msg));
                                ErrorLog.Write(exc.ToString());
                            }
                            //networkServer.EventFromClient(e);
                            break;
                        case NetMessageType.DISCONNECT:
                            //netStream.Close(0);
                            System.Console.WriteLine("NetworkServerConnectionHandler.ConnectionHandler:connection closed");
                            //netStream.Dispose();
                            //networkServer.RemoveClient(eventDistClient.id);
                            sendThread.Abort();
                            ForceClose();
                            return;
                        case NetMessageType.NONE:
                            ErrorLog.Write(String.Format("NONFATAL Deserialize Error in NetworkServer: {0}", m.msg));
                            ErrorLog.Write(String.Format("TYPE: {0}; MSG: {1};",m.type, m.msg));
                            break;
                        default:
                            throw new Exception("connection handler got an invalid event");
                            

                    }
                }
                catch (System.IO.IOException exc)
                {
                    System.Console.WriteLine("NetworkServerConnectionHandler.ConnectionHandler:lost connection with client");
                    ForceClose();
                    return;
                }
                catch (System.ObjectDisposedException)
                {
                    return;
                }
                catch (ThreadAbortException)
                {
                    return;
                }
                catch (Exception exc)
                {
                    MessageBox.Show("An error has occured in the Simulation Server.\nPlease email the C:\\DDDErrorLog.txt file to Aptima customer support with a description of what you were doing at the time of the error.");
                    ErrorLog.Write(exc.ToString() + "\n");
                    throw new Exception();
                }

            }
        }
示例#3
0
 internal void SendDisconnect()
 {
     NetMessage m = new NetMessage();
     m.type = NetMessageType.DISCONNECT;
     m.clientID = eventDistClient.id;
     m.Send(ref netStream,String.Empty);
 }
示例#4
0
        public void SendEvents()
        {
            NetMessage m = new NetMessage();
            List<SimulationEvent> events = null;
            int count = 0;
            while (true)
            {
                try
                {
                    if (eventDistClient != null)
                    {
                        events = eventDistClient.GetEvents();
                        foreach (SimulationEvent e in events)
                        {
                            m.type = NetMessageType.EVENT;
                            m.clientID = eventDistClient.id;
                            try
                            {
                                m.msg = SimulationEventFactory.XMLSerialize(e);
                            }
                            catch (Exception exc)
                            {
                                ErrorLog.Write(String.Format("NONFATAL Serialize Error in NetworkServer: {0}", e.eventType));
                                ErrorLog.Write(exc.ToString());
                                continue;
                            }
                            m.Send(ref netStream,String.Empty);
                        }
                        Thread.Sleep(50);
                        count += 1;

                        if (count >= 100)
                        {

                            count = 0;
                            m.type = NetMessageType.PING;
                            m.clientID = eventDistClient.id;
                            m.Send(ref netStream,String.Empty);
                        }
                    }
                }
                catch (System.IO.IOException exc)
                {
                    ForceClose();
                    return;
                }
                catch (System.ObjectDisposedException)
                {
                    return;
                }
                catch (ThreadAbortException)
                {
                    return;
                }
                catch (Exception exc)
                {
                    MessageBox.Show("An error has occured in the Simulation Server.\nPlease email the C:\\DDDErrorLog.txt file to Aptima customer support with a description of what you were doing at the time of the error.");
                    ErrorLog.Write(exc.ToString() + "\n");
                    throw new Exception();
                }
            }
        }
示例#5
0
 /// <summary>
 /// This method is used to subscribe to event types.
 /// GetEvents will only return events that have been subscribed to with this method.
 /// </summary>
 /// <param name="eventName">The name of an event type that is defined in the simulation model</param>
 public void Subscribe(string eventName)
 {
     if (isConnected)
     {
         NetMessage m = new NetMessage();
         m.type = NetMessageType.SUBSCRIBE;
         m.msg = eventName;
         m.clientID = clientID;
         m.Send(ref netStream, TerminalID);
     }
     else
     {
         throw new Exception("NetworkClient: Must be connected first");
     }
 }
示例#6
0
 /// <summary>
 /// This method is used to send an event to the DDD Server.
 /// </summary>
 /// <param name="e">The event</param>
 public void PutEvent(SimulationEvent e)
 {
     if (isConnected)
     {
         NetMessage m = new NetMessage();
         m.type = NetMessageType.EVENT;
         try
         {
             m.msg = SimulationEventFactory.XMLSerialize(e);
         }
         catch (Exception exc)
         {
             ErrorLog.Write(String.Format("NONFATAL Serialize Error in NetworkClient: {0}", e.eventType));
             ErrorLog.Write(exc.ToString());
             return;
         }
         m.clientID = clientID;
         m.Send(ref netStream, TerminalID);
     }
 }
示例#7
0
        /// <summary>
        /// This method is used to connect to the DDD Server.
        /// </summary>
        /// <param name="servername">The hostname of the machine the DDD Server is running on</param>
        /// <param name="port">The port number that the DDD Server is listening on</param>
        /// <returns>boolean connect success</returns>
        public bool Connect(string servername, int port)
        {
            try
            {
                if (!isConnected)
                {
                    client = new TcpClient(servername, port);
                    netStream = client.GetStream();
                    NetMessage m = new NetMessage();
                    m.type = NetMessageType.REGISTER;
                    m.Send(ref netStream, TerminalID);
                    m.Receive(ref netStream);
                    clientID = m.clientID;


                    clientThread = new Thread(new ThreadStart(SocketHandler));
                    clientThread.Start();
                    isConnected = true;
                    return true;
                }
                else
                {
                    throw new Exception("NetworkClient: already connected");
                }
            }
            catch (System.Net.Sockets.SocketException e)
            {
                System.Console.WriteLine(e.ToString());
                System.Console.WriteLine(e.Message);
                return false;

            }

        }
示例#8
0
        /// <summary>
        /// Disconnect from the DDD Server.
        /// This should always be called before a client application closes 
        /// in order to gracefully disconnect from the server.
        /// </summary>
        public void Disconnect()
        {
            if (isConnected)
            {
                
                NetMessage m = new NetMessage();
                m.type = NetMessageType.DISCONNECT;
                m.Send(ref netStream,TerminalID);
                
                netStream.Close();
                netStream.Dispose();
                isConnected = false;
                clientThread.Abort();
                //clientThread.Suspend();
            }

        }