コード例 #1
0
        private HandleResult ServerReceive(IServer sender, IntPtr connId, byte[] data)
        {
            var extra = sender.GetExtra <TcpPortForwardingExtra>(connId);

            if (extra == null)
            {
                return(HandleResult.Error);
            }

            if (!extra.Agent.Send(extra.AgentConnId, data, data.Length))
            {
                return(HandleResult.Error);
            }

            return(OnServerReceive?.Invoke(sender, connId, data) ?? HandleResult.Ok);
        }
コード例 #2
0
ファイル: Server.cs プロジェクト: Vergervan/brainblo-library
            private void ClientHandler <M>(Socket clientSocket)
            {
                clientList.Add(clientSocket);
                OnServerAccept?.Invoke(this, new AcceptEventArgs(clientSocket));
                int    fullMessageSize;
                string fullMessage;

                byte[] messageBuffer = new byte[1024];
                while (true)
                {
                    fullMessage     = string.Empty;
                    fullMessageSize = 0;
                    try
                    {
                        do
                        {
                            int messageSize = clientSocket.Receive(messageBuffer);
                            fullMessageSize += messageSize;
                            fullMessage     += Encoding.UTF8.GetString(messageBuffer, 0, messageSize);
                        } while (clientSocket.Available > 0);
                        OnServerReceive?.Invoke(this, new EventArgs());
                    }
                    catch (Exception exception)
                    {
                        OnReceiveException?.Invoke(this, new ExceptionEventArgs(clientSocket, exception));
                        break;
                    }

                    List <ByteArray> byteArrays = Buffer.SplitBuffer(Encoding.UTF8.GetBytes(fullMessage), 0);

                    object message = default;
                    foreach (var byteArray in byteArrays)
                    {
                        if (typeof(M) != typeof(string))
                        {
                            message = JsonSerializer.Deserialize <M>(Encoding.UTF8.GetString(byteArray.bytes));
                        }
                        else
                        {
                            message = Encoding.UTF8.GetString(byteArray.bytes);
                        }
                        MessageProcessing?.Invoke(this, new MessageProcessEventArgs(new MessageData(message, fullMessageSize, fullMessage)));
                    }
                }
            }