Example #1
0
        public void ConnectionEstablished(object Sender, SocketAsyncEventArgs e)
        {
            var connectionMessageInfo = new SocketAsyncEventArgsContainer(e.Buffer,
                                                                          e.Offset, e.BytesTransferred, e.SocketError, ((Peer)e.UserToken).peerIP,
                                                                          ((Peer)e.UserToken).peerName);

            if (this.InvokeRequired)
            {
                this.Invoke(new ShowSocketEventInfo(ShowConnectionMsg), connectionMessageInfo);
            }
            else
            {
                ShowConnectionMsg(connectionMessageInfo);
            }

            if (e.SocketError == SocketError.Success)
            {
                AddPeer((Peer)e.UserToken);
                ((Peer)e.UserToken).ReceiveMessage();
            }
            else
            {
                ((Peer)e.UserToken).Close();
            }
        }
Example #2
0
        public void MessageSent(object Sender, SocketAsyncEventArgs e)
        {
            var messageSentMessageInfo = new SocketAsyncEventArgsContainer(e.Buffer,
                                                                           e.Offset, e.BytesTransferred, e.SocketError, (IPEndPoint)e.RemoteEndPoint, ((Peer)e.UserToken).peerName);

            if (this.InvokeRequired)
            {
                this.Invoke(new ShowSocketEventInfo(ShowMessageSentMessage), messageSentMessageInfo);
            }
            else
            {
                ShowMessageSentMessage(messageSentMessageInfo);
            }

            switch (e.SocketError)
            {
            case (SocketError.Success):
                break;

            default:
                ((Peer)e.UserToken).Close();
                RemovePeer((Peer)e.UserToken);
                break;
            }
        }
Example #3
0
        private void ShowMessageReceivedMessage(SocketAsyncEventArgsContainer messageInfo)
        {
            switch (messageInfo.errorStatus)
            {
            case (SocketError.Success):
                if (messageInfo.buffer.Length == 0)
                {
                    tbMessages.AppendText($"{messageInfo.peerName}  disconnected!" + TextBoxNL);
                }
                else
                {
                    string recvdMessage = Encoding.ASCII.GetString(messageInfo.buffer).TrimEnd('\0');
                    if (recvdMessage.Length > 0)
                    {
                        tbMessages.AppendText($"{messageInfo.peerName} ({messageInfo.peerIP.Address.ToString()}) says:" + TextBoxNL +
                                              Encoding.ASCII.GetString(messageInfo.buffer).TrimEnd('\0') + TextBoxNL);
                    }
                }
                break;

            case (SocketError.Disconnecting):
                tbMessages.AppendText($"{messageInfo.peerName} disconnected!" + TextBoxNL);
                break;

            default:
                tbMessages.AppendText($"Connection with {messageInfo.peerName} has failed with code " +
                                      (long)messageInfo.errorStatus + TextBoxNL);
                break;
            }
        }
Example #4
0
        public bool MessageReceivedSync(object Sender, SocketAsyncEventArgs e)
        {
            var messageRecievedMessageInfo = new SocketAsyncEventArgsContainer(e.Buffer,
                                                                               e.Offset, e.BytesTransferred, e.SocketError, ((Peer)e.UserToken).peerIP,
                                                                               ((Peer)e.UserToken).peerName);

            if (this.InvokeRequired)
            {
                this.Invoke(new ShowSocketEventInfo(ShowMessageReceivedMessage), messageRecievedMessageInfo);
            }
            else
            {
                ShowMessageReceivedMessage(messageRecievedMessageInfo);
            }

            switch (e.SocketError)
            {
            case (SocketError.Success):
                if (e.BytesTransferred == 0)
                {
                    ((Peer)e.UserToken).Close();
                    RemovePeer((Peer)e.UserToken);
                    return(false);
                }
                break;

            default:
                ((Peer)e.UserToken).Close();
                RemovePeer((Peer)e.UserToken);
                return(false);
            }
            return(true);
        }
Example #5
0
        private void ShowMessageSentMessage(SocketAsyncEventArgsContainer messageInfo)
        {
            switch (messageInfo.errorStatus)
            {
            case (SocketError.Success):
                break;

            case (SocketError.Disconnecting):
                tbMessages.AppendText($"{messageInfo.peerName} disconnected!" + TextBoxNL);
                break;

            default:
                tbMessages.AppendText($"Connection with {messageInfo.peerName} has failed with code " +
                                      (long)messageInfo.errorStatus + TextBoxNL);
                break;
            }
        }
Example #6
0
        public void ShowConnectionMsg(SocketAsyncEventArgsContainer messageInfo)
        {
            long connectionStatus = (long)messageInfo.errorStatus;

            if (connectionStatus == 0)
            {
                tbMessages.AppendText($"Connection with {messageInfo.peerName} " +
                                      $"({messageInfo.peerIP.Address.ToString()})" +
                                      "is established" + TextBoxNL);;;

                btnSend.Enabled = true;
            }
            else
            {
                tbMessages.AppendText($"Connection with {messageInfo.peerName} " +
                                      $"({messageInfo.peerIP.Address.ToString()}) "
                                      + $"has failed with code {connectionStatus}" + TextBoxNL);
            }
        }