Exemplo n.º 1
0
        private void AcceptCallback(IAsyncResult ar)
        {
            Socket sock = ServerSocket.EndAccept(ar);

            ClientSockets.Add(sock);
            sock.BeginReceive(Buffer, 0, 100, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
        }
Exemplo n.º 2
0
        private async void OnConnection(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
        {
            Writer = null;
            ClientSockets.Add(args.Socket);

            var reader = new DataReader(args.Socket.InputStream);

            try
            {
                while (Working)
                {
                    var sizeFaildCount = await reader.LoadAsync(sizeof(uint));

                    var stringLength       = reader.ReadUInt32();
                    var actualStringLength = await reader.LoadAsync(stringLength);

                    if (sizeFaildCount != sizeof(uint) || actualStringLength != stringLength)
                    {
                        reader.DetachStream();
                        reader.Dispose();
                        ClientSockets?.Remove(args.Socket);
                        return;
                    }

                    var dataArrary = new byte[actualStringLength];
                    reader.ReadBytes(dataArrary);
                    var dataJson = Encoding.UTF8.GetString(dataArrary);
                    var data     = JsonConvert.DeserializeObject <MessageModel>(dataJson);
                    await SendMsg(data, args.Socket);

                    MsgReceivedAction?.Invoke(data);
                }
            }
            catch (Exception e)
            {
                if (SocketError.GetStatus(e.HResult) == SocketErrorStatus.Unknown)
                {
                }
                Debug.WriteLine(string.Format("Received data: \"{0}\"", "Read stream failed with error: " + e.Message));
                reader.DetachStream();
                reader.Dispose();
                ClientSockets?.Remove(args.Socket);
            }
        }
Exemplo n.º 3
0
        private void AcceptCallback(IAsyncResult ar)
        {
            Socket socket;

            try
            {
                socket = ServerSocket.EndAccept(ar);
            }
            catch (ObjectDisposedException) // I cannot seem to avoid this (on exit when properly closing sockets)
            {
                return;
            }

            ClientSockets.Add(socket);
            socket.BeginReceive(Buffer, 0, BufferSize, SocketFlags.None, ReceiveCallback, socket);
            // Console.WriteLine("Client connected, waiting for request...");
            ClientConnected?.Invoke();
            ServerSocket.BeginAccept(AcceptCallback, null);
        }
Exemplo n.º 4
0
        // SUMMARY: On client connected
        protected virtual void AcceptCallback(IAsyncResult AR)
        {
            Socket socket = ServerSocket.EndAccept(AR);

            try
            {
                lock (this.LockObj)
                    ClientSockets.Add(new SocketObject(socket));
                Console.WriteLine("Client connected");

                socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), socket);
                ServerSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
                this.OnClientConnected(this[socket].Identifier);
            }
            catch (Exception e)
            {
                this.OnClientDisconnected(this[socket].Identifier);
                lock (this.LockObj)
                    OnSocketFatalDisconection(e, socket);
            }
        }
Exemplo n.º 5
0
        private void AcceptCallback(IAsyncResult AR)
        {
            lock (lockAccept)
            {
                Socket socket;

                try
                {
                    socket = ServerSocket.EndAccept(AR);
                    var session = new SocketSession(socket, BufferSize);

                    ClientSockets.Add(session);

                    socket.BeginReceive(session.SessionStorage, 0, BufferSize, SocketFlags.None, ReceiveCallback, socket);
                    ServerSocket.BeginAccept(AcceptCallback, null);
                }
                catch (Exception ex) // I cannot seem to avoid this (on exit when properly closing sockets)
                {
                    LogController.WriteLog(new ServerLog("*** ERROR ***: \n" + ex.Message, ServerLogType.ERROR));
                }
            }
        }
Exemplo n.º 6
0
        private void AcceptCallBack(IAsyncResult asyncResult)
        {
            if (!IsRunning)
            {
                return;
            }

            TCPConnection client = new TCPConnection();

            client.Socket = serverSocket.EndAccept(asyncResult);

            if (!client.Socket.Connected)
            {
                return;
            }


            ClientSockets.Add(client.Socket.RemoteEndPoint, client);

            OnClientConnected?.Invoke(client.Socket.RemoteEndPoint);

            serverSocket.BeginAccept(new AsyncCallback(AcceptCallBack), client);
        }