Пример #1
0
        protected virtual void HandleConnection(object socket)
        {
            listenerSignal.Set();
            try
            {
                Socket handler = (Socket)socket;
                while (true)
                {
                    receiveHandle = new SocketHandle(handler);

                    while (true)
                    {
                        int bytesReceived = receiveHandle.socket.Receive(receiveHandle.buffer);
                        receiveHandle.AppendBytes(bytesReceived);

                        if (receiveHandle.EndOfMessage() == "<EOF>")
                        {
                            break;
                        }
                    }
                    EventHandler <MessageEventArgs> messageReceived = MessageReceived;
                    if (messageReceived != null)
                    {
                        messageReceived.Invoke(null, new MessageEventArgs(receiveHandle.fullBuffer, receiveHandle.endOfMessage));
                    }
                }
            }
            catch (SocketException ex)
            {
                Console.WriteLine(ex.ToString());
                throw ex;
            }
        }
Пример #2
0
        public void StartClient(IPAddress ipAdd)
        {
            ipAddress = ipAdd;
            IPEndPoint endPoint = new IPEndPoint(ipAdd, 11001);

            Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            sendSocket = new SocketHandle(client);

            try
            {
                sendSocket.socket.BeginConnect(endPoint, new AsyncCallback(ConnectCallBack), sendSocket.socket);
                ConnectDone.WaitOne();

                EventHandler connectedHandler = ClientConnected;
                if (connectedHandler != null)
                {
                    connectedHandler(null, EventArgs.Empty);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                throw ex;
            }
        }
Пример #3
0
 public void AddClient(ObjectId userId, SocketHandle clientHandle)
 {
     lock (clients)
     {
         clients[userId] = clientHandle;
     }
 }
Пример #4
0
        public void ClientMessageReceived(object sender, AndroidMessageEventArgs args)
        {
            if (!(args.message is DestroyedBuffer))
            {
                SocketHandle clientHandle = (SocketHandle)sender;
                UserMessage  userMessage  = args.message as UserMessage;
                ObjectId     userId       = userMessage.userid;

                AddClient(userId, clientHandle);

                Message response = null;
                Console.WriteLine("[Received {0}]  {1}", userMessage.username, args.message.GetType().ToString());

                if (args.message is SyncRequest)
                {
                    SyncRequest syncRequest = args.message as SyncRequest;
                    response = syncRequest.RetrieveData();
                }
                else if (args.message is CreateUser)
                {
                    CreateUser newUserMessage = args.message as CreateUser;
                    response = newUserMessage.Create();
                }
                else if (args.message is AddLinkFeed)
                {
                    AddLinkFeed linkFeed = args.message as AddLinkFeed;
                    response = linkFeed.AddLink();
                }
                else if (args.message is AddTextUpdate)
                {
                    AddTextUpdate textUpdate = args.message as AddTextUpdate;
                    response = textUpdate.AddLink();
                }
                else if (args.message is ChangePassword)
                {
                    ChangePassword changePassword = args.message as ChangePassword;
                    response = changePassword.Start();
                }
                else if (args.message is ModifyItem)
                {
                    ModifyItem modifyItem = args.message as ModifyItem;
                    response = modifyItem.Modify();
                }
                else if (args.message is DeleteUser)
                {
                    DeleteUser deleteUser = args.message as DeleteUser;
                    response = deleteUser.Delete();
                }

                Console.WriteLine("\t[Sent {0}]  {1}", response.GetType().ToString(), userMessage.username);
                byte[] bson = BSON.Serialize <Message>(response);
                Send(clientHandle, bson);
            }
        }
Пример #5
0
        protected virtual void ConnectCallBack(IAsyncResult result)
        {
            listenerSignal.Set();

            try
            {
                Socket       listener     = (Socket)result.AsyncState;
                Socket       handler      = listener.EndAccept(result);
                SocketHandle clientSocket = new SocketHandle(handler);

                clientSocket.socket.BeginReceive(clientSocket.buffer, 0, SocketHandle.BUFFER_SIZE, 0,
                                                 new AsyncCallback(ReceiveCallBack), clientSocket);
            }
            catch (SocketException ex)
            {
                Console.WriteLine(ex.ToString());
                throw ex;
            }
        }
Пример #6
0
        private void SendCallBack(IAsyncResult result)
        {
            SocketHandle clientHandle = (SocketHandle)result.AsyncState;

            try
            {
                clientHandle.socket.EndSend(result);

                // TODO: Event ?

                clientHandle.socket.Shutdown(SocketShutdown.Both);
                clientHandle.socket.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                throw ex;
            }
        }
Пример #7
0
        private void ReceiveCallBack(IAsyncResult result)
        {
            SocketHandle socketHandle = (SocketHandle)result.AsyncState;
            Socket       client       = socketHandle.socket;
            int          bytesRead    = socketHandle.socket.EndReceive(result);

            try
            {
                if (bytesRead > 0)
                {
                    socketHandle.AppendBytes(bytesRead);

                    if (client.Available > 0)
                    {
                        client.BeginReceive(socketHandle.buffer, 0, SocketHandle.BUFFER_SIZE, 0,
                                            new AsyncCallback(ReceiveCallBack), socketHandle);
                    }
                    else
                    {
                        if (socketHandle.fullBuffer.Length > 1)
                        {
                            EventHandler <AndroidMessageEventArgs> messageReceived = MessageReceived;
                            if (messageReceived != null)
                            {
                                messageReceived.BeginInvoke(socketHandle,
                                                            new AndroidMessageEventArgs(socketHandle.fullBuffer, socketHandle.EndOfMessage()),
                                                            OnEventFinished, null);
                            }

                            socketHandle.Reset();
                        }
                    }
                }
            }
            catch (SocketException ex)
            {
                Console.WriteLine(ex.ToString());
                throw ex;
            }
        }
Пример #8
0
        protected void Receive(IAsyncResult result)
        {
            try
            {
                if (result != null)
                {
                    var async         = (System.Runtime.Remoting.Messaging.AsyncResult)result;
                    var invokedMethod = (EventHandler <MessageEventArgs>)async.AsyncDelegate;
                    invokedMethod.EndInvoke(result);
                }

                Socket server = receiveSocket.socket;
                receiveSocket = new SocketHandle(server);
                receiveSocket.socket.BeginReceive(receiveSocket.buffer, 0, SocketHandle.BUFFER_SIZE, 0,
                                                  new AsyncCallback(ReceiveCallBack), receiveSocket);
            }
            catch (SocketException ex)
            {
                Console.WriteLine(ex.ToString());
                throw ex;
            }
        }
Пример #9
0
        private void ReceiveCallBack(IAsyncResult result)
        {
            SocketHandle socketHandle = (SocketHandle)result.AsyncState;
            Socket       server       = socketHandle.socket;
            int          bytesRead    = socketHandle.socket.EndReceive(result);

            try
            {
                if (bytesRead > 0)
                {
                    //socketHandle.byteStream.Write(socketHandle.buffer, 0, bytesRead);

                    if (server.Available > 0)
                    {
                        server.BeginReceive(socketHandle.buffer, 0, SocketHandle.BUFFER_SIZE, 0,
                                            new AsyncCallback(ReceiveCallBack), socketHandle);
                    }
                    else
                    {
                        //if (socketHandle.byteStream.Length > 1)
                        {
                            EventHandler <MessageEventArgs> messageReceived = MessageReceived;
                            if (messageReceived != null)
                            {
                                //messageReceived.Invoke(null, new MessageEventArgs(socketHandle.byteStream.ToArray()));
                                // messageReceived.BeginInvoke(null, new MessageEventArgs(socketHandle.byteStream.ToArray(), socketHandle.endOfMessage), Receive, null);
                            }

                            socketHandle.Reset();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                throw ex;
            }
        }
Пример #10
0
        protected void Send(SocketHandle clientHandle, byte[] data)
        {
            byte[] beginning = Encoding.UTF8.GetBytes("<BOF>");
            byte[] ending    = Encoding.UTF8.GetBytes("<EOF>");
            byte[] message   = new byte[beginning.Length + data.Length + ending.Length];
            Buffer.BlockCopy(beginning, 0, message, 0, beginning.Length);
            Buffer.BlockCopy(data, 0, message, beginning.Length, data.Length);
            Buffer.BlockCopy(ending, 0, message, beginning.Length + data.Length, ending.Length);

            if (IsConnected(clientHandle))
            {
                clientHandle.socket.BeginSend(message, 0, message.Length, 0, new AsyncCallback(SendCallBack), clientHandle);
            }
            else
            {
                //this.sendSocket.Shutdown(SocketShutdown.Both);
                //this.sendSocket.Close();
                //this.StartClient(ipAddress);

                Send(clientHandle, data);
                throw new Exception();
            }
        }
Пример #11
0
 private bool IsConnected(SocketHandle clientHandle)
 {
     return(!(clientHandle.socket.Poll(1000, SelectMode.SelectRead) && clientHandle.socket.Available == 0));
 }