public ClientForServer()
        {
            GetMessageFromServer = new AddMessageDelegate(AsyncReceiveMessage);

            System.Threading.TimerCallback timerCallback = new System.Threading.TimerCallback(timerCallbackMethod);
            timer = new System.Threading.Timer(timerCallback, null, System.Threading.Timeout.Infinite, TimerTickPeriod);
        }
 private ErrorBox()
 {
     InitializeComponent();
     this.Text = Properties.Resources.ApplicationErrorTitle;
     addMessageHandler = new AddMessageDelegate(this.AddMessage);
     CreateHandle();
     labelCommonError.Text = Properties.Resources.ApplicationErrorText;
 }
Exemple #3
0
        public MyTransportLayer(int channel)
        {
            m_channel       = channel;
            m_sendStream    = new ByteStream(64 * 1024, true);
            m_receiveStream = new ByteStream();

            m_loopback = MyNetworkReader.SetHandler(channel, HandleMessage, MyReceiveQueue.Mode.Timer, Timer);
        }
        public MyTransportLayer(int channel)
        {
            m_handlers.Add(MyMessageId.OLD_GAME_EVENT, HandleOldGameEvent);

            m_channel       = channel;
            m_sendStream    = new ByteStream(64 * 1024, true);
            m_receiveStream = new ByteStream();

            m_loopback = MyNetworkReader.SetHandler(channel, HandleMessage, MyReceiveQueue.Mode.Timer, Timer);
        }
Exemple #5
0
		public OutputPanel(IManager manager)
		{
			// This call is required by the Windows Form Designer.
			InitializeComponent();

			// TODO: Add any initialization after the InitializeComponent call

			mAddMessageDelegate = new AddMessageDelegate(AddMessage);
			mClearLogDelegate = new ClearLogDelegate(ClearLog);

			mTemplateLog.Parent.Controls.Remove(mTemplateLog);
		}
Exemple #6
0
 /// <summary>
 /// メッセージ追加
 /// </summary>
 /// <param name="str">追加文字列</param>
 public void AddMessage(string str)
 {
     if (textBoxMessage.InvokeRequired)
     {
         AddMessageDelegate d = new AddMessageDelegate(AddMessage);
         Invoke(d, new object[] { str });
     }
     else
     {
         textBoxMessage.AppendText(DateTime.Now.ToLongTimeString() + "   " + str + "\r\n");
     }
 }
Exemple #7
0
 public void AddMessage(string message)
 {
     if (richTextBox1.InvokeRequired)
     {
         AddMessageDelegate d = AddMessage;
         richTextBox1.Invoke(d, message);
     }
     else
     {
         richTextBox1.AppendText(message);
     }
 }
Exemple #8
0
 public void AddMessage(string message, Label lbtemp)
 {
     if (lbtemp.InvokeRequired)
     {
         AddMessageDelegate d = AddMessage;
         lbtemp.Invoke(d, message, lbtemp);
     }
     else
     {
         lbtemp.Text = message;
     }
 }
Exemple #9
0
 // Thread safety
 public void AddMessage(String Message)
 {
     if (MsgsBox.InvokeRequired)
     {
         AddMessageDelegate dlgt = new AddMessageDelegate(AddMessage);
         Invoke(dlgt, Message);
     }
     else
     {
         MsgsBox.Text += Message;
     }
 }
Exemple #10
0
        public OutputPanel(IManager manager)
        {
            // This call is required by the Windows Form Designer.
            InitializeComponent();

            // TODO: Add any initialization after the InitializeComponent call

            mAddMessageDelegate = new AddMessageDelegate(AddMessage);
            mClearLogDelegate   = new ClearLogDelegate(ClearLog);

            mTemplateLog.Parent.Controls.Remove(mTemplateLog);
        }
Exemple #11
0
 private void AddMessage(string text)
 {
     if (this.InvokeRequired)
     {
         var d = new AddMessageDelegate(AddMessage);
         this.BeginInvoke(d, new object[] { text });
     }
     else
     {
         MessageText.AppendText(text.Replace("\n", Environment.NewLine));
         MessageText.AppendText(Environment.NewLine);
     }
 }
Exemple #12
0
 private void AddMessageToListBox(MobilePhoneClassLib.Message msg)
 {
     if (this.MessageListBox.InvokeRequired && this.MessageListBox != null)
     {
         AddMessageDelegate d = new AddMessageDelegate(AddMessageToListBox);
         this.Invoke(d, new object[] { msg });
     }
     else
     {
         string text = "Text: " + msg.Text + " From: " + msg.SenderNumber;
         this.MessageListBox.Items.Add(text);
     }
 }
Exemple #13
0
 private void AddMessage(TextBox txtbox, string message)
 {
     if (txtbox.InvokeRequired)
     {
         AddMessageDelegate addMessageDelegate = AddMessage;
         txtbox.Invoke(addMessageDelegate, new object[] { txtbox, message });
     }
     else
     {
         txtbox.Text += "\r\n" + message;
         txtbox.Select(txtbox.TextLength, 0); //光标定位到文本最后
         txtbox.ScrollToCaret();              //滚动到光标
     }
 }
        private void runServer(object obj)
        {
            ThreadServerParam param = obj as ThreadServerParam;

            string role = "Server";
            NetworkServer n = new NetworkServer(this.ipAddress, this.port);

            int maxPlayersToWaitFor = MAX_PLAYERS_TO_WAIT_FOR;
            int i = 1;
            AddAutentizationDelegate addPlayer = new AddAutentizationDelegate(AddPlayer);
            AddMessageDelegate addMessage = new AddMessageDelegate(AddMessage);
            ConnectionEstablished threadStoppedDel = new ConnectionEstablished(threadStopped);

            if (this.ipAddress == "Automatic")
            {
                Dispatcher.Invoke(addMessage, "Listening on port: " + this.port);
            }
            else
            {
                Dispatcher.Invoke(addMessage, "Listening on: " + this.ipAddress + ":" + this.port);
            }

            bool isAutenticated = false;

            try
            {
                while (i <= maxPlayersToWaitFor && endTheThread != 1)
                {
                    isAutenticated = false;
                    DebuggerIX.WriteLine(DebuggerTag.Net, role, "Initialization # " + i + " invoked");
                    Dispatcher.Invoke(addMessage, "Attempt to receive connection #" + i);

                    try
                    {
                        n.InitializeConnection();
                    }
                    catch (InvalidStateException e)
                    {
                        DebuggerIX.WriteLine(DebuggerTag.Net, role, "InvalidState: " + e.Message);
                        Dispatcher.Invoke(addMessage, "Error:" + e.Message);
                        n.CloseConnection();
                    }
                    catch (TimeoutException e)
                    {
                        DebuggerIX.WriteLine(DebuggerTag.Net, role, "TimeOut: " + e.Message);
                        Dispatcher.Invoke(addMessage, "Timeout for attempt #" + i);
                        n.CloseConnection();
                    }

                    DebuggerIX.WriteLine(DebuggerTag.Net, role, "Initialization = " + n.IsInitialized);

                    if (n.IsInitialized == true)
                    {
                        DebuggerIX.WriteLine(DebuggerTag.Net, role, "Sending 'Autentization'");
                        n.SendAsync(NetworkMessageType.Authentication, param.Authentication);
                        n.AllSentHandle.WaitOne(); // wait until all is sent
                        DebuggerIX.WriteLine(DebuggerTag.Net, role, "'Autentization' sent");
                        Dispatcher.Invoke(addMessage, "Handshake sent");

                        bool disconnect = false;
                        Authentication auth = null;

                        while (isAutenticated == false && disconnect == false)
                        {
                            DebuggerIX.WriteLine(DebuggerTag.Net, role, "Start async receiving");
                            n.ReceiveAsync();
                            DebuggerIX.WriteLine(DebuggerTag.Net, role, "Waiting for a message to be received");
                            n.ReceivedMessageHandle.WaitOne(5000);

                            NetworkMessageType messageType = n.GetReceivedMessageType();
                            DebuggerIX.WriteLine(DebuggerTag.Net, role, "Received message: " + messageType);

                            if (messageType == NetworkMessageType.Authentication)
                            {
                                auth = (Authentication)n.GetReceivedMessageFromQueue();
                                DebuggerIX.WriteLine(DebuggerTag.Net, role,
                                    string.Format("'Autentization' message details: {0}, {1}", auth.Name, auth.IP));

                                Dispatcher.Invoke(addPlayer, auth);
                                Dispatcher.Invoke(addMessage, "Autentization message received");

                                if (param.AutomaticFirstConnect)
                                {
                                    Dispatcher.Invoke(addMessage, "Accepting game opponent.");
                                    isAutenticated = true;

                                    DebuggerIX.WriteLine(DebuggerTag.Net, role, "Sending 'StartGame' message");
                                    n.SendAsync(NetworkMessageType.StartGame);
                                    n.AllSentHandle.WaitOne();
                                    DebuggerIX.WriteLine(DebuggerTag.Net, role, "'StartGame' sent");
                                }
                            }
                            else if (messageType == NetworkMessageType.DisconnectRequest)
                            {
                                DisconnectRequest dr = (DisconnectRequest)n.GetReceivedMessageFromQueue();
                                DebuggerIX.WriteLine(DebuggerTag.Net, role, "'DisconnectRequest' was sent by the other side in: " +
                                    dr.DateTime);

                                DebuggerIX.WriteLine(DebuggerTag.Net, role, "Sending 'DisconnectRequestConfirmation' message");
                                n.SendAsync(NetworkMessageType.DisconnectRequestConfirmation);
                                n.AllSentHandle.WaitOne();
                                DebuggerIX.WriteLine(DebuggerTag.Net, role, "'DisconnectRequestConfirmation' sent");
                                disconnect = true;
                                Dispatcher.Invoke(addMessage, "Send request to end handshake");
                            }
                        }

                        if (disconnect)
                        {
                            n.CloseConnection();
                            Dispatcher.Invoke(addMessage, "End of initial handshake");
                            DebuggerIX.WriteLine(DebuggerTag.Net, role, "Connection closed.");
                        }

                        if (isAutenticated)
                        {
                            Dispatcher.Invoke(new EstablishConnectionDelegate(establishConnection),
                                new object[] { n, auth});
                            break; // from outer while cyclus
                        }
                    }

                    i++;
                }

                if (isAutenticated == false)
                {
                    Dispatcher.Invoke(addMessage, "All attempts tried. Click 'listen' to begin again.");
                }
            }
            catch (ThreadInterruptedException)
            {
                DebuggerIX.WriteLine(DebuggerTag.Net, role, "Thread interrupted.");
                n.CloseConnection();
            }
            finally
            {
                if (isAutenticated == false)
                {
                    n.CloseConnection();
                }

                Dispatcher.Invoke(addMessage, "Network connection searching was disabled.");
                DebuggerIX.WriteLine(DebuggerTag.Net, role, "Finished");
                Dispatcher.Invoke(threadStoppedDel, new object[] {isAutenticated});
            }
        }
 public void AddMessage(MessageSource source, String msg)
 {
     if (!MessageBlock.Dispatcher.CheckAccess())
     {
         AddMessageDelegate del = new AddMessageDelegate(AddMessage);
         MessageBlock.Dispatcher.Invoke(del, new object[] { source, msg });
     }
     else
     {
         String adding = "";
         switch (source)
         {
             case MessageSource.Info:
                 adding += "[INFO] " + msg;
                 break;
             case MessageSource.Local:
                 adding += "[LOCAL] " + msg;
                 break;
             case MessageSource.Error:
                 adding += "[ERROR] " + msg;
                 break;
             case MessageSource.Remote:
                 adding += "[REMOTE] " + msg;
                 break;
             default:
                 break;
         }
         adding += "\n";
         MessageBlock.AppendText(adding);
         MessageBlock.CaretIndex = MessageBlock.Text.Length;
         MessageBlock.ScrollToEnd();
         //MessageBlock.Text += adding;
     }
 }
Exemple #16
0
        private void runClient(object obj)
        {
            ThreadClientParam param = obj as ThreadClientParam;

            string role = "Client";
            NetworkClient n = new NetworkClient(this.ipAddress, this.port);
            ConnectionEstablished threadStoppedDel = new ConnectionEstablished(threadStopped);

            int i = 1;
            AddMessageDelegate addMessage = new AddMessageDelegate(AddMessage);
            Dispatcher.Invoke(addMessage, "Connecting to: " + this.ipAddress + ":" + this.port);

            bool startGameMessageReceived = false;
            bool wasGameStarted = false;
            int maxNumberOfConnectionAttempts = 3;

            try
            {
                while (i <= maxNumberOfConnectionAttempts && endTheThread != 1)
                {
                    startGameMessageReceived = false;
                    DebuggerIX.WriteLine(DebuggerTag.Net, role, "Initialization # " + i + " invoked");
                    Dispatcher.Invoke(addMessage, "Attempt to receive connection #" + i);

                    try
                    {
                        n.InitializeConnection();
                    }
                    catch (InvalidStateException e)
                    {
                        DebuggerIX.WriteLine(DebuggerTag.Net, role, "InvalidState: " + e.Message);
                        Dispatcher.Invoke(addMessage, "Error:" + e.Message);
                        continue;
                    }
                    catch (TimeoutException e)
                    {
                        DebuggerIX.WriteLine(DebuggerTag.Net, role, "TimeOut: " + e.Message);
                        Dispatcher.Invoke(addMessage, "Timeout for attempt #" + i);
                        continue;
                    }

                    DebuggerIX.WriteLine(DebuggerTag.Net, role, "Initialization = " + n.IsInitialized);

                    if (n.IsInitialized == false)
                    {
                        Dispatcher.Invoke(addMessage, n.ErrorMessage);
                    }
                    else
                    {
                        DebuggerIX.WriteLine(DebuggerTag.Net, role, "Sending 'Autentization'");
                        n.SendAsync(NetworkMessageType.Authentication, param.Authentication);

                        try
                        {
                            n.AllSentHandle.WaitOne(3000); // wait until all is sent
                        }
                        catch (TimeoutException)
                        {
                            DebuggerIX.WriteLine(DebuggerTag.Net, role, "Timeout");
                            Dispatcher.Invoke(addMessage, "Timeout");
                            continue;
                        }

                        DebuggerIX.WriteLine(DebuggerTag.Net, role, "'Autentization' sent");
                        Dispatcher.Invoke(addMessage, "Handshake sent");

                        bool disconnect = false;
                        Authentication auth = null;
                        int noneMessageNo = 0;

                        while (startGameMessageReceived == false && disconnect == false && n.IsInitialized == true)
                        {
                            DebuggerIX.WriteLine(DebuggerTag.Net, role, "Start async receiving");
                            n.ReceiveAsync();
                            DebuggerIX.WriteLine(DebuggerTag.Net, role, "Waiting for a message to be received");

                            try
                            {
                                n.ReceivedMessageHandle.WaitOne(2500);
                            }
                            catch (TimeoutException)
                            {
                                DebuggerIX.WriteLine(DebuggerTag.Net, role, "Timeout");
                                Dispatcher.Invoke(addMessage, "Timeout");
                                break;
                            }

                            NetworkMessageType messageType = n.GetReceivedMessageType();
                            DebuggerIX.WriteLine(DebuggerTag.Net, role, "Received message: " + messageType);
                            object message = null;

                            if (messageType != NetworkMessageType.None)
                            {
                                noneMessageNo = 0;
                                message = n.GetReceivedMessageFromQueue();
                            }

                            if (messageType == NetworkMessageType.Authentication)
                            {
                                auth = (Authentication)message;
                                DebuggerIX.WriteLine(DebuggerTag.Net, role,
                                    string.Format("'Autentization' message details: {0}, {1}", auth.Name, auth.IP));

                                Dispatcher.Invoke(addMessage, "Autentization message received");

                            }
                            else if (messageType == NetworkMessageType.DisconnectRequest)
                            {
                                DisconnectRequest dr = (DisconnectRequest)message;
                                DebuggerIX.WriteLine(DebuggerTag.Net, role, "'DisconnectRequest' was sent by the other side in: " +
                                    dr.DateTime);

                                DebuggerIX.WriteLine(DebuggerTag.Net, role, "Sending 'DisconnectRequestConfirmation' message");
                                n.SendAsync(NetworkMessageType.DisconnectRequestConfirmation);
                                n.AllSentHandle.WaitOne();
                                DebuggerIX.WriteLine(DebuggerTag.Net, role, "'DisconnectRequestConfirmation' sent");
                                disconnect = true;
                                Dispatcher.Invoke(addMessage, "Send request to end handshake");

                            }
                            else if (messageType == NetworkMessageType.StartGame)
                            {
                                startGameMessageReceived = true;
                            }
                            else if (messageType == NetworkMessageType.None)
                            {
                                noneMessageNo++;

                                if (noneMessageNo > 2)
                                {
                                    Dispatcher.Invoke(addMessage, "No message was received in three attempts in a row. Giving up.");
                                    break;
                                }
                                else
                                {
                                    Dispatcher.Invoke(addMessage, "No message was received.");
                                }
                            }
                            else
                            {
                                Dispatcher.Invoke(addMessage, "Warning: Unknown message received.");
                            }
                        }

                        if (n.IsInitialized == false)
                        {
                            Dispatcher.Invoke(addMessage, "Connection lost.");
                            DebuggerIX.WriteLine(DebuggerTag.Net, role, "Connection lost.");
                        }

                        if (disconnect)
                        {
                            n.CloseConnection();
                            Dispatcher.Invoke(addMessage, "End of initial handshake");
                            DebuggerIX.WriteLine(DebuggerTag.Net, role, "Connection closed.");
                        }

                        if (startGameMessageReceived && auth != null)
                        {
                            wasGameStarted = true;
                            Dispatcher.Invoke(addMessage, "Game is about to start.");
                            Dispatcher.Invoke(new EstablishConnectionDelegate(establishConnection),
                                new object[] { n, auth });
                            break; // from outer while cyclus
                        }
                    }

                    i++;
                }

                if (wasGameStarted == false)
                {
                    DebuggerIX.WriteLine(DebuggerTag.Net, role, "All attempts tried. Click 'Again' to begin again.");
                    Dispatcher.Invoke(addMessage, "All attempts tried. Click 'Again' to begin again.");
                }
                else
                {
                    Dispatcher.BeginInvoke(threadStoppedDel, new object[] { wasGameStarted });
                }
            }
            catch (ThreadInterruptedException e)
            {
                n.CloseConnection();
                DebuggerIX.WriteLine(DebuggerTag.Net, role, "Thread interrupted. Message: " + e.Message);
                Dispatcher.Invoke(addMessage, "Connection attempts aborted by user request.");
            }
            finally
            {

                if (wasGameStarted == false)
                {
                    n.CloseConnection();
                    Dispatcher.Invoke(addMessage, "Network connection searching was disabled.");
                }
            }
            DebuggerIX.WriteLine(DebuggerTag.Net, role, "Finished");
        }
Exemple #17
0
 private void InitData()
 {
     //_client= new TcpClient();
     addMessage =new AddMessageDelegate(AddMessageInvoke);
     updateDataSource=new UpdateDataSourceDelegate(UpdateClientListInvoke);
     addStatusMessage=new AddMessageDelegate(AddStatusMessageInvoke);
     setControlEnable= new SetControlEnableDelegate(SetControlEnableInvoke);
     //clearText=new ClearTextDelegate(ClearTextInvoke);
     setText =new SetTextDelegate(SetTextInvoke);
 }
Exemple #18
0
 private void InitData()
 {
     listBoxClientList.SelectionMode=SelectionMode.MultiSimple;
     numericUpDownPort.Maximum = 9999;
     numericUpDownPort.Value = 2016;
     numericUpDownPort.Minimum = 1;
     addMessage = new AddMessageDelegate(AddMessageInvoke);
     addMessageToControl=new AddMessageToControlDelegate(AddMessageToControlInvoke);
     updateDataSource = new UpdateDataSourceDelegate(UpdateClientListInvoke);
     setText = new SetTextDelegate(SetTextInvoke);
     listBoxClientList.DataSource = _dicClient.Keys.ToList();
 }