Exemplo n.º 1
0
        void Close()
        {
            if (!IsConnected)
            {
                PostMessage("Not connected.");
                return;
            }

            if (tcp_socket_.IsEmpty)
            {
                PPBUDPSocket.Close(udp_socket_);
                udp_socket_.Dispose();
            }
            else
            {
                PPBTCPSocket.Close(tcp_socket_);
                tcp_socket_.Dispose();
            }

            PostMessage("Closed connection.");
        }
Exemplo n.º 2
0
        private void OnReadCompletion(PPError result)
        {
            var status = string.Empty;

            if ((int)result <= 0)
            {
                if ((int)result == 0)
                {
                    status = "server: client disconnected";
                }
                else
                {
                    status = $"server: Read failed: {result}";
                }
                instance.PostMessage(status);

                // Remove the current incoming socket and try
                // to accept the next one.
                PPBTCPSocket.Close(incomingSocket);
                incomingSocket.Dispose();
                TryAccept();
                return;
            }

            status = $"server: Read {(int)result} bytes";
            instance.PostMessage(status);

            // Echo the bytes back to the client
            result = (PPError)PPBTCPSocket.Write(incomingSocket,
                                                 receiveBuffer,
                                                 (int)result,
                                                 new CompletionCallback(OnWriteCompletion));

            if (result != PPError.OkCompletionpending)
            {
                instance.PostMessage($"server: Write failed: {result}");
            }
        }