Exemple #1
0
        public void Deserialize(byte[] data)
        {
            BinaryFormatter formatter = new BinaryFormatter();

            using (MemoryStream ms = new MemoryStream())
            {
                Int32 size = BitConverter.ToInt32(data, 1);
                Console.WriteLine(size);
                ms.Write(data, 4, size - 5);
                ms.Position = 0;
                NUserJoin user = new NUserJoin();
                user          = (NUserJoin)formatter.Deserialize(ms);
                this.userID   = user.userID;
                this.username = user.username;
            }
        }
Exemple #2
0
        private void OnUserAck(IAsyncResult ar)
        {
            State     state = (State)ar.AsyncState;
            TcpClient tcp   = state.tcp;

            byte[] buffer = state.buffer;

            if (buffer[0] == (byte)32)
            {
                Log("Got user ack");

                string name   = null;
                string letter = null;
                for (int i = 2; i < buffer.Length && (letter = ((char)buffer[i]).ToString()) != "\0"; i++)
                {
                    name += letter;
                }

                if (!String.IsNullOrWhiteSpace(name))
                {
                    User user = new User(tcp)
                    {
                        Username = name,
                        UserID   = userID
                    };
                    users.Add(user);

                    NUserJoin notification = new NUserJoin
                    {
                        userID   = userID,
                        username = name
                    };
                    userID++;
                    SendToAll(notification.Serialize());

                    UserState uState = new UserState
                    {
                        user = user
                    };
                    user.Connection.Client.BeginReceive(uState.buffer, 0, uState.buffer.Length, SocketFlags.None, new AsyncCallback(OnUserData), uState);
                }
            }
        }
Exemple #3
0
        private void OnReceive(IAsyncResult ar)
        {
            byte[] buffer = (byte[])ar.AsyncState;
            Log("BUFFER FLAG " + buffer[0].ToString());
            switch (buffer[0])
            {
            case 25:
                Log("Received ack from server");
                byte[] ack = { 32 };
                using (MemoryStream ms = new MemoryStream())
                {
                    ms.Write(ack, 0, 1);
                    string name = "Testing";
                    foreach (char c in name)
                    {
                        ms.WriteByte((byte)c);
                    }
                    Send(ms.ToArray());
                }
                break;

            case 1:
                Log("Join notification received");
                Log(buffer.Length.ToString());
                NUserJoin notification = new NUserJoin();
                notification.Deserialize(buffer);
                Log("Username: "******"User ID: " + notification.userID);
                break;

            default:
                break;
            }

            connection.Client.EndReceive(ar);
            Receive();
        }