コード例 #1
0
 private static void OnAttemptConnect(IAsyncResult ar)
 {
     try
     {
         Status = true;
         s_socket.EndConnect(ar); //notify the server the connection was established succefully
         MessageStructure msgToSend = new MessageStructure
         {
             Command    = Command.AttemptLogin,
             Color      = HexConverter.Convert(Client.Color),
             ClientName = Client.Name,
             UserName   = Client.UserName
         };
         byte[] msgToSendByte = msgToSend.ToByte();
         // Ssend the login credinails of the established connection to the server
         //s_socket.BeginSend(msgToSendByte, 0, msgToSendByte.Length, SocketFlags.None, OnSend, null);
         s_socket.Send(msgToSendByte, 0, msgToSendByte.Length, SocketFlags.None);
         s_byteMessage = new byte[2097152];
         s_socket.BeginReceive(s_byteMessage, 0, s_byteMessage.Length, SocketFlags.None, OnReceive, null);
     }
     catch (Exception ex)
     {
         if (ClientNetworkEngineAttemptLoginErrorEvent != null)
         {
             ClientNetworkEngineAttemptLoginErrorEvent.Invoke(ex.Message);
         }
         //MessageBox.Show(ex.Message + @" -> OnAttemptConnect", @"Chat: ", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
コード例 #2
0
        private static void OnReceive(IAsyncResult ar)
        {
            if (!Status)
            {
                return;
            }
            try
            {
                // Let the server know the message was recieved
                s_socket.EndReceive(ar);
                // Convert message from bytes to messageStracure class and store it in msgReceieved
                MessageStructure msgReceived = new MessageStructure(s_byteMessage);
                // Set new bytes and start recieving again
                s_byteMessage = new byte[2097152];
                if (msgReceived.Command == Command.Disconnect)
                {
                    Status = false;
                    MessageStructure msgToSend = new MessageStructure
                    {
                        Command    = Command.Disconnect,
                        ClientName = Client.Name,
                        UserName   = Client.UserName
                    };
                    byte[] b = msgToSend.ToByte();
                    s_socket.Send(b, 0, b.Length, SocketFlags.None);
                    s_socket.Shutdown(SocketShutdown.Both);
                    s_socket.BeginDisconnect(true, (OnDisonnect), s_socket);
                    if (ClientNetworkEngineDisconnectEvent != null)
                    {
                        ClientNetworkEngineDisconnectEvent.Invoke();
                    }
                    return;
                }
                if (msgReceived.Command != Command.AttemptLogin && msgReceived.Command != Command.Register)
                {
                    s_socket.BeginReceive(s_byteMessage, 0, s_byteMessage.Length, SocketFlags.None, OnReceive, s_socket);
                }

                switch (msgReceived.Command)
                {
                case Command.Register:
                    if (ClientNetworkEngineRegisterMessageEvent != null)
                    {
                        ClientNetworkEngineRegisterMessageEvent.Invoke(msgReceived.Message);
                    }
                    break;

                case Command.AttemptLogin:
                    if (ClientNetworkEngineAttemptLoginErrorEvent != null)
                    {
                        ClientNetworkEngineAttemptLoginErrorEvent.Invoke(msgReceived.Message);
                    }
                    break;

                case Command.Login:
                    if (msgReceived.ClientName == Client.Name)
                    {
                        if (ClientNetworkEngineLoggedinEvent != null)
                        {
                            ClientNetworkEngineLoggedinEvent.Invoke();
                        }
                        // Send Request for online client list
                        MessageStructure msgToSend = new MessageStructure
                        {
                            Command    = Command.List,
                            ClientName = Client.Name
                        };
                        byte[] byteMessageToSend = msgToSend.ToByte();
                        s_socket.BeginSend(byteMessageToSend, 0, byteMessageToSend.Length, SocketFlags.None, OnSend, s_socket);
                        return;
                    }
                    if (ClientNetworkEngineLoginEvent != null)
                    {
                        ClientNetworkEngineLoginEvent.Invoke(msgReceived.ClientName, msgReceived.Message);
                    }
                    break;

                case Command.List:
                    if (ClientNetworkEngineClientsListEvent != null)
                    {
                        ClientNetworkEngineClientsListEvent.Invoke(msgReceived.Message);
                    }
                    break;

                case Command.Logout:
                    if (ClientNetworkEngineLogoutEvent != null)
                    {
                        ClientNetworkEngineLogoutEvent.Invoke(msgReceived.ClientName, msgReceived.Message);
                    }
                    break;

                case Command.Message:
                    if (ClientNetworkEngineMessageEvent != null)
                    {
                        ClientNetworkEngineMessageEvent.Invoke(msgReceived.ClientName, msgReceived.Message, ColorTranslator.FromHtml(msgReceived.Color));
                    }
                    break;

                case Command.NameChange:
                    if (Client.Name == msgReceived.ClientName)
                    {
                        Client.Name = msgReceived.Message;
                    }
                    if (ClientNetworkEngineNameChangeEvent != null)
                    {
                        ClientNetworkEngineNameChangeEvent.Invoke(msgReceived.ClientName, msgReceived.Message, ColorTranslator.FromHtml(msgReceived.Color));
                    }
                    break;

                case Command.ColorChanged:
                    if (ClientNetworkEngineColorChangedEvent != null)
                    {
                        ClientNetworkEngineColorChangedEvent.Invoke(msgReceived.ClientName, ColorTranslator.FromHtml(msgReceived.Color));
                    }
                    break;

                case Command.PrivateStart:
                    if (ClientNetworkEnginePrivateChatStartEvent != null)
                    {
                        ClientNetworkEnginePrivateChatStartEvent.Invoke(msgReceived.ClientName);
                    }
                    break;

                case Command.PrivateMessage:
                    if (ClientNetworkEnginePrivateMessageEvent != null)
                    {
                        ClientNetworkEnginePrivateMessageEvent.Invoke(msgReceived.ClientName, msgReceived.Private, msgReceived.Message);
                    }
                    break;

                case Command.PrivateStopped:
                    if (ClientNetworkEnginePrivateStoppedEvent != null)
                    {
                        ClientNetworkEnginePrivateStoppedEvent.Invoke(msgReceived.ClientName);
                    }
                    //TabPagePrivateChatReceiveClientEvent.Invoke(msgReceived.ClientName, msgReceived.Private, msgReceived.Message, 1);
                    break;

                case Command.ServerMessage:
                    if (ClientNetworkEngineServerMessageEvent != null)
                    {
                        ClientNetworkEngineServerMessageEvent.Invoke(msgReceived.Message);
                    }
                    break;

                case Command.ImageMessage:
                    MemoryStream ms  = new MemoryStream(msgReceived.ImgByte);
                    Image        img = Image.FromStream(ms);
                    if (ClientNetworkEngineImageMessageEvent != null)
                    {
                        ClientNetworkEngineImageMessageEvent.Invoke(msgReceived.ClientName, msgReceived.Private, img);
                    }
                    break;
                }
            }
            //catch (ArgumentException)
            //{
            //    //MessageBox.Show(ex.Message + @" -> OnReceive", @"Chat: " + Client.Name, MessageBoxButtons.OK, MessageBoxIcon.Error);
            //}
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + @" -> OnReceive", @"Chat: " + Client.Name, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }