Exemplo n.º 1
0
        private static Packet RequestChannelAccess(Packet p)
        {
            try
            {
                ClientRequestChannelAccess packet = new ClientRequestChannelAccess();
                Console.WriteLine(packet.id);
                Channel channel = Channel.GetChannelById(packet.id);
                channel?.AddUser(packet.SenderID);

                ServerAcknowledgementRequestChannelAccess response = new ServerAcknowledgementRequestChannelAccess();
                response.success = true;
                response.id      = channel.id;

                return(response);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            return(null);
        }
Exemplo n.º 2
0
        public static void StartClient()
        {
            // Connect to a remote device.
            try {
                // Establish the remote endpoint for the socket.
                // The name of the
                // set to localhostd
                IPHostEntry ipHostInfo = Dns.GetHostEntry("127.0.0.1");
                IPAddress   ipAddress  = ipHostInfo.AddressList[0];
                IPEndPoint  remoteEP   = new IPEndPoint(ipAddress, port);

                // Create a TCP/IP socket.
                client = new Socket(ipAddress.AddressFamily,
                                    SocketType.Stream, ProtocolType.Tcp);

                // Connect to the remote endpoint.
                client.BeginConnect(remoteEP,
                                    new AsyncCallback(ConnectCallback), client);
                connectDone.WaitOne();

                // Send test data to the remote device.
                StateObject state = new StateObject();
                //SET UP THREAD for receive
                Thread receiveLoop = new Thread(new ThreadStart(() => {
                    try{
                        while (end)
                        {
                            Receive(client);
                            receiveDone.WaitOne();
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.ToString());
                    }
                }));
                receiveLoop.Start();



                //TEST SECTION
                ClientAccountRegister register = new ClientAccountRegister();
                register.login    = "******";
                register.name     = "user";
                register.password = "******";
                Send(client, Packet.Encode(PacketType.ClientAccountRegister, (ICanSerialize)register));
                sendDone.WaitOne();

                // register.login = "******";
                // register.name = "user2";
                // register.password = "******";
                // Send(client, Packet.Encode(PacketType.ClientAccountRegister, (ICanSerialize)register));
                // sendDone.WaitOne();

                // Console.WriteLine("Continue ...");
                // Console.ReadLine();

                ClientAccountLogin login = new ClientAccountLogin();
                login.login    = "******";
                login.password = "******";
                Send(client, Packet.Encode(PacketType.ClientAccountLogin, (ICanSerialize)login));
                sendDone.WaitOne();
                Console.WriteLine("Sent login.");

                Console.WriteLine("Continue ...");
                Console.ReadLine();

                ClientRequestChannelList requestList = new ClientRequestChannelList();
                Send(client, Packet.Encode(PacketType.ClientRequestChannelList, (ICanSerialize)requestList));
                sendDone.WaitOne();
                Console.WriteLine("Sent list request.");
                Console.ReadLine();


                ClientRequestChannelAccess request = new ClientRequestChannelAccess();
                request.id       = 1;
                request.SenderID = Session.currentUser.id;
                Send(client, Packet.Encode(PacketType.ClientRequestChannelAccess, (ICanSerialize)request));
                sendDone.WaitOne();
                Console.WriteLine("Sent request.");

                // Console.WriteLine("ANY KEY TO TERMINATE ...");
                // Console.ReadLine();

                PacketSendMessage p = new PacketSendMessage();
                p.ChannelID = 1;
                p.message   = "test";
                p.userID    = 1;

                // Send(client, Packet.Encode(PacketType.ClientSendMessage, (ICanSerialize)p));
                // sendDone.WaitOne();

                // Release the socket.
                Console.WriteLine("ANY KEY TO TERMINATE ...");
                Console.ReadLine();
                end = false;
                client.Shutdown(SocketShutdown.Both);
                client.Close();
            } catch (Exception e) {
                Console.WriteLine(e.ToString());
            }
        }