예제 #1
0
        public void ClientLogic()
        {
            // ping
            SprotoObject request = Proto.C2S.NewSprotoObject("C2GS_Ping.request");

            request["str"] = "hello,world!";
            RpcPackage Package = Proto.PackRequest("C2GS_Ping", request);

            TcpSocket.Send(Package.data, Package.size);
        }
예제 #2
0
        public void SendMessage(int id, byte[] messageBytes)
        {
            if (IsConnected)
            {
                byte[] idBytes = BitConverter.GetBytes(id);
                if (messageBytes != null && messageBytes.Length > 0)
                {
                    byte[] bytes = new byte[sizeof(int) + messageBytes.Length];
                    Array.Copy(idBytes, 0, bytes, 0, idBytes.Length);
                    Array.Copy(messageBytes, 0, bytes, idBytes.Length, messageBytes.Length);

                    m_ClientSocket.Send(bytes);
                }
                else
                {
                    m_ClientSocket.Send(idBytes);
                }
            }
        }
예제 #3
0
        // パケット送信/応答待ち
        private void sendPacketWaitResponse(int val)
        {
            // パケット作成
            var packet = new PacketData(3);

            packet.SetChar(0, 'D');
            packet.SetHex(1, 2, val);

            // パケット送信
            socket.Send(packet);

            // 表示更新
            sendPackNum++;
            this.BeginInvoke((Action)(() => {
                textBox1.Text = sendPackNum.ToString();
            }));

            // パケット受信
            var resPacket = socket.WaitPacket(500);

            // 応答はあったか?
            if (resPacket != null)
            {
                // ACK応答か?
                if (resPacket.Data[0] == AsciiCode.ACK)
                {
                    recvAckNum++;
                }
                else
                {
                    recvNakNum++;
                }
            }
            else
            {
                recvNoneNum++;
            }
            // 表示更新
            this.BeginInvoke((Action)(() => {
                updateCounter();
            }));
        }
예제 #4
0
        /// <summary>
        /// Sent client request object to remote service……
        /// </summary>
        /// <param name="Packaged">packaged data.</param>
        /// <returns>System.Collection.Generic.KeyValuePair</returns>
        public KeyValuePair <string, object> Request(IList <object> Packaged)
        {
            if (Packaged != null && ClientRequest != null && ClientRequest.IsConnected && !running)
            {
                //write the sending/receive data to log
                //ClientRequest.Iswrite = RenbarGUI.Properties.Settings.Default.IsWriteDataLog;

                // change signal status ..
                running = true;

                // send request object to remote server ..
                ClientRequest.Send(this.EnvSvr.Serialize(Packaged));

                // wait for remote response ……
                object response = this.EnvSvr.Deserialize(ClientRequest.Receive());

                if (response != null)
                {
                    // change signal status ..
                    running = false;

                    // convert correct data type ..
                    return((KeyValuePair <string, object>)response);
                }
                else
                {
                    // change signal status ..
                    running = false;

                    return(new KeyValuePair <string, object>(string.Format("-Err {0}", DateTime.Now), null));
                }
            }
            else
            {
                return(new KeyValuePair <string, object>(string.Format("-Err {0}", DateTime.Now), null));
            }
        }
예제 #5
0
 void onConnected(TcpClientSocket client)
 {
     client.Send(Encoding.UTF8.GetBytes(_command));
 }
예제 #6
0
        public void TcpTest()
        {
            object monitor = new object();

            lock (monitor) {
                bool clientMaySend  = false;
                int  payloadCounter = 0;

                Console.WriteLine("Starting server");
                HashSet <Socket> connectedSockets = new HashSet <Socket>();
                TcpServerSocket  server           = null;
                server = new TcpServerSocket(socket => {
                    lock (monitor) {
                        lock (connectedSockets) {
                            Console.WriteLine("Server received client's connection");
                            connectedSockets.Add(socket);
                        }
                    }
                }, (sender, buffer, size) => {
                    lock (monitor) {
                        Console.WriteLine("SRec " + size);
                        server.Send(sender, buffer, 0, size);
                    }
                }, socket => {
                    lock (connectedSockets) {
                        Console.WriteLine("Server lost connection to client");
                        connectedSockets.Remove(socket);
                    }
                }, connectedSockets, 1, Port);

                byte[] sendBuffer = new byte[DataSize];
                _random.NextBytes(sendBuffer);

                Console.WriteLine("Starting client");
                TcpClientSocket client = null;
                client = new TcpClientSocket(() => {
                    lock (monitor) {
                        Console.WriteLine("Client connected to server");
                        if (clientMaySend)
                        {
                            Console.WriteLine("Client not waiting for main thread");
                        }
                        else
                        {
                            Console.WriteLine("Client waiting for main thread");
                            Monitor.Wait(monitor);
                        }
                        Console.WriteLine("Client sending first data");
                        client.Send(sendBuffer, 0, sendBuffer.Length);
                    }
                }, error => {
                    lock (monitor) {
                        Console.WriteLine("Client failed to connect to server: " + error);
                    }
                }, (buffer, size) => {
                    lock (monitor) {
                        Console.WriteLine("CRec " + size);
                        AssertArrayContentsEqualInFirstArrayLengthRange(sendBuffer, buffer);
                        if (++payloadCounter == PayloadCount)
                        {
                            Monitor.Pulse(monitor);
                        }
                        else
                        {
                            _random.NextBytes(sendBuffer);
                            client.Send(sendBuffer, 0, sendBuffer.Length);
                        }
                    }
                }, () => Console.WriteLine("Client lost connection to server"));
                client.Start(Ip, Port);

                clientMaySend = true;
                Monitor.Pulse(monitor);
                Console.WriteLine("Main thread waiting");
                Monitor.Wait(monitor);

                Console.WriteLine("Closing client");
                client.Close();
                Console.WriteLine("Closing server");
                server.Close();
            }
        }
예제 #7
0
 static void onConnected(TcpClientSocket client)
 {
     Debug.Log("Connected.");
     Debug.Log("Generating...");
     client.Send(Encoding.UTF8.GetBytes("gen"));
 }