Esempio n. 1
0
 static void Client(IPAddress address)
 {
     Console.Clear();
     t              = new SowixTransport.Transport();
     t.transport    = new SowixTransport.TCPTransport();
     messageChannel = t.AddChannel(SowixTransport.ChannelType.Reliable);
     t.Connect(address.ToString(), 2137);
     new Thread(ClientRecvThread).Start();
     position = Console.WindowTop;
     while (true)
     {
         int x = Console.CursorLeft;
         int y = Console.CursorTop;
         Console.SetCursorPosition(0, Console.WindowHeight - 1);
         Console.Write(">                                                  ");
         Console.SetCursorPosition(1, Console.WindowHeight - 1);
         string str     = Console.ReadLine();
         byte[] message = Encoding.UTF8.GetBytes(str);
         Console.SetCursorPosition(x, y);
         if (str == "/exit")
         {
             t.Disconnect();
             return;
         }
         else if (str.StartsWith("/nickname "))
         {
             t.Send("Nickname", Encoding.UTF8.GetBytes(str.Substring(10)), 0, messageChannel);
         }
         else if (str == "/users")
         {
             t.Send("GetUsers", new byte[1], 0, messageChannel);
         }
         else
         {
             t.Send("Message", message, 0, messageChannel);
         }
         Thread.Sleep(100);
     }
 }
Esempio n. 2
0
        static void Server()
        {
            t              = new SowixTransport.Transport();
            t.transport    = new SowixTransport.TCPTransport();
            messageChannel = t.AddChannel(SowixTransport.ChannelType.SequencedReliable);

            t.Bind(2137);

            List <string> Nicknames = new List <string>();

            while (true)
            {
                var events = t.Update();
                foreach (var item in events)
                {
                    Console.WriteLine($"Received event {item.Type} type:{item.PacketType} from {item.Peer}");
                    switch (item.Type)
                    {
                    case SowixTransport.EventType.PeerConnected:
                        Nicknames.Add($"User {item.Peer}");
                        for (int i = 0; i < t.Peers.Count; i++)
                        {
                            t.Send("SMessage", Encoding.UTF8.GetBytes($"Connected {Nicknames[i]}"), i, messageChannel);
                        }
                        break;

                    case SowixTransport.EventType.Data:
                        if (item.PacketType == "Nickname")
                        {
                            if (Nicknames.Contains(Encoding.UTF8.GetString(item.Data)))
                            {
                                t.Send("SMessage", Encoding.UTF8.GetBytes("This username is already taken"), item.Peer, messageChannel);
                            }
                            else
                            {
                                for (int i = 0; i < t.Peers.Count; i++)
                                {
                                    t.Send("SMessage", Encoding.UTF8.GetBytes($"{Nicknames[item.Peer]} changed nickname to {Encoding.UTF8.GetString(item.Data)}"), i, messageChannel);
                                }
                                Nicknames[item.Peer] = Encoding.UTF8.GetString(item.Data);
                            }
                        }
                        if (item.PacketType == "Message")
                        {
                            string message = $"{Nicknames[item.Peer]}: " + Encoding.UTF8.GetString(item.Data);
                            for (int i = 0; i < t.Peers.Count; i++)
                            {
                                t.Send("Message", Encoding.UTF8.GetBytes(message), i, messageChannel);
                            }
                        }
                        if (item.PacketType == "GetUsers")
                        {
                            string a = "";
                            for (int i = 0; i < t.Peers.Count; i++)
                            {
                                var user = t.Peers[i];
                                if (user.Status != SowixTransport.PeerStatus.Connected)
                                {
                                    continue;
                                }
                                a += $"{user.EndPoint} as {Nicknames[i]}\n";
                            }
                            t.Send("SMessage", Encoding.UTF8.GetBytes("User list:\n" + a), item.Peer, messageChannel);
                        }
                        break;

                    case SowixTransport.EventType.PeerDisconnected:
                        for (int i = 0; i < t.Peers.Count; i++)
                        {
                            t.Send("SMessage", Encoding.UTF8.GetBytes($"Disconnected {Nicknames[i]}"), i, messageChannel);
                        }
                        break;

                    default:
                        break;
                    }
                }
                Thread.Sleep(100);
            }
        }