示例#1
0
        /// <summary>
        /// Entry Point to start Network and Active Loop
        /// </summary>
        public void Start()
        {
            isServer = ip.Length <= 0 ? true : false;
            if (isServer)
            {
                try
                {
                    OnChatRecieved?.Invoke(this, "Waiting client to Connect");
                    // Server Machine
                    // Id = 1
                    listener = new TcpListener(IPAddress.Any, port);
                    listener.Start();
                    client = listener.AcceptTcpClient();
                    OnChatRecieved?.Invoke(this, "server Created");
                    netStream = client.GetStream();
                    listener.Stop();
                    networkActive = true;

                    // Set Id of this Machine
                    Properties.Settings.Default.userId = 1;
                }
                catch (Exception e)
                {
                    OnChatRecieved?.Invoke(this, "Connecting Stoped");
                    try { listener.Stop(); } catch { }
                    //OnError?.Invoke(this, e.Message);
                    networkActive    = false;
                    otherSideClosing = true;
                }
            }
            else
            {
                try
                {
                    // Client Machine
                    // Id = 1
                    client    = new TcpClient(ip, port);
                    netStream = client.GetStream();
                    OnChatRecieved?.Invoke(this, "Client Created");
                    netStream = client.GetStream();
                    // Set Id of this Machine
                    networkActive = true;
                    Properties.Settings.Default.userId = -1;
                }
                catch (Exception e)
                {
                    OnError?.Invoke(this, e.Message);
                    networkActive    = false;
                    otherSideClosing = true;
                }
            }
            if (client != null && networkActive)
            {
                HandShake();
                Network_loop();
            }
            closeConnection();
        }
示例#2
0
 /// <summary>
 /// When the Network is done call CloseConnection To clean up
 /// reasources
 /// </summary>
 public void closeConnection()
 {
     if (!otherSideClosing)
     {
         sendData = "Close|true";
         send();
     }
     try
     {
         OnChatRecieved?.Invoke(this, "Connection Closed");
         netStream?.Close();
         netStream = null;
         client.Close();
         client.Dispose();
     }
     catch (Exception e) { Console.WriteLine(e.ToString()); }
     OnDisconnect?.Invoke(this, new EventArgs());
     networkActive    = false;
     otherSideClosing = false;
 }
示例#3
0
        //extract out to seperate class commands
        // net work is just send receive
        private void Execute_Commands(string data, string pretext)
        {
            string[] command = data.Split('~');
            for (int i = 0; i < command.Length; i++)
            {
                string[] cmd = command[i].Split(paramDelimiter);
                switch (cmd[0])
                {// constatnats any litral the data is in the code
                case "Chat":
                    if (cmd[2].Length > 0)
                    {    // send obj not primatives obj validation or some function
                        if (pretext == "Sent")
                        {
                            pretext = "You";
                        }                                              //Properties.Settings.Default.userName; }
                        else
                        {
                            pretext = cmd[1];
                        }
                        OnChatRecieved?.Invoke(this, pretext + cmd[2]);
                    }
                    break;

                case "Move":
                    OnMoveRecieved?.Invoke(this, new MoveArgs(int.Parse(cmd[2]), int.Parse(cmd[1])));
                    break;

                case "StartGame":
                    OnNewGame?.Invoke(this, GameState.GetGameState(cmd[1]));
                    break;

                case "Close":
                    networkActive    = false;
                    otherSideClosing = true;
                    break;
                }
            }
        }