示例#1
0
        private OTcpServerHeader SendHeader(ref TcpClient client, int messageSize, string command, string expectType, Guid token)
        {
            var header      = new OTcpServerHeader(Commands.GetHashOfCommand(command), messageSize, expectType, token);
            var headerBytes = ObjectBinarySerialization.ObjectToByteArray(header);

            client.GetStream().Write(headerBytes, 0, headerBytes.Length);
            return(header);
        }
示例#2
0
        private void SendBinary <T>(string command, T messageData)
        {
            var data       = ObjectBinarySerialization.ObjectToByteArray(messageData);
            var header     = new OTcpServerHeader(Commands.GetHashOfCommand(command), data.LongLength, (messageData).GetType().FullName);
            int headerSize = OTcpServerHeader.HeaderSize;

            _currentStream.Write(ObjectBinarySerialization.ObjectToByteArray(header), 0, headerSize);
            _currentStream.Write(data, 0, data.Length); // Write the bytes
        }
示例#3
0
 private void SendBinary <T>(ref TcpClient client, T message, string command, Guid token)
 {
     try
     {
         var messageBytes = ObjectBinarySerialization.ObjectToByteArray(message);
         var messageSize  = messageBytes.Length;
         SendHeader(ref client, messageSize, command, message.GetType().FullName, token);
         // token handle
         client.GetStream().Write(messageBytes, 0, messageSize);
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
         throw e;
     }
 }
示例#4
0
 private void SendJSON <T>(string command, T messageData)
 {
     try
     {
         var data       = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(messageData));
         var header     = new OTcpServerHeader(Commands.GetHashOfCommand(command), data.LongLength, (messageData).GetType().FullName);
         int headerSize = OTcpServerHeader.HeaderSize;
         _currentStream.Write(ObjectBinarySerialization.ObjectToByteArray(header), 0, headerSize);
         _currentStream.Write(data, 0, data.Length); // Write the bytes
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
         throw e;
     }
 }
示例#5
0
        private OTcpServerMessage ReceiveBinary(ref TcpClient client)
        {
            var header          = ReceiveHeader(ref client);
            var receivedMessage = new OTcpServerMessage();

            if (header == null)
            {
                // LOG
                return(null);
            }

            receivedMessage.Header = header;
            byte[] messageBytes = new byte[header.DataSize]; // Clear the message
            // Receive the stream of bytes
            client.GetStream().Read(messageBytes, 0, messageBytes.Length);
            receivedMessage.Message = ObjectBinarySerialization.ByteArrayToObject(messageBytes);
            return(receivedMessage);
        }
示例#6
0
        private OTcpServerHeader ReceiveHeader(ref TcpClient client)
        {
            int headerSize = OTcpServerHeader.HeaderSize;

            byte[] headerBytes = new byte[headerSize]; // Clear the message
                                                       // Receive the stream of bytes
            try
            {
                _currentStream.Read(headerBytes, 0, headerSize);
                OTcpServerHeader header = (OTcpServerHeader)ObjectBinarySerialization.ByteArrayToObject(headerBytes);
                header.ExpectType = header.ExpectType.Split(' ')[0];
                return(header);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return(null);
            }
        }
示例#7
0
        private OTcpServerHeader ReceiveHeader(ref TcpClient client)
        {
            // Get Header
            var headerByteSize = OTcpServerHeader.HeaderSize;

            byte[] headerbuffer = new byte[headerByteSize];
            //client.Client.RemoteEndPoint
            try
            {
                client.GetStream().Read(headerbuffer, 0, headerbuffer.Length);
                OTcpServerHeader receivedHeader = (OTcpServerHeader)ObjectBinarySerialization.ByteArrayToObject(headerbuffer);
                receivedHeader.ExpectType = receivedHeader.ExpectType.Split(' ')[0];
                return(receivedHeader);
            }
            catch (Exception e)
            {
                Console.WriteLine("Header Receive Error" + e.Message);
                throw e;
            }
        }
示例#8
0
        //public void AddCommand(string commandName, FunctionHandler.ObjInObjOutHandlerDelegate responseFunction)
        //{
        //    if (commandName != null && responseFunction != null)
        //    {

        //        var fh  = new FunctionHandler(commandCount,commandName,FunctionHandlerType.ObjInObjOut,responseFunction);
        //        CommandList.Add(commandCount, fh);
        //        commandCount++;
        //    }
        //}
        //public void AddCommand(string commandName, FunctionHandler.ObjInVoidOutHandlerDelegate responseFunction)
        //{
        //    if (commandName != null && responseFunction != null)
        //    {

        //        var fh = new FunctionHandler(commandCount, commandName, FunctionHandlerType.ObjInVoidOut, responseFunction);
        //        CommandList.Add(commandCount, fh);
        //        commandCount++;
        //    }
        //}
        //public void AddCommand(string commandName, FunctionHandler.VoidInObjOutHandlerDelegate responseFunction)
        //{
        //    if (commandName != null && responseFunction != null)
        //    {

        //        var fh = new FunctionHandler(commandCount, commandName, FunctionHandlerType.VoidInObjOut, responseFunction);
        //        CommandList.Add(commandCount, fh);
        //        commandCount++;
        //    }
        //}
        //public void AddCommand(string commandName, FunctionHandler.VoidInVoidOutHandlerDelegate responseFunction)
        //{
        //    if (commandName != null && responseFunction != null)
        //    {

        //        var fh = new FunctionHandler(commandCount, commandName, FunctionHandlerType.VoidInVoidOut, responseFunction);
        //        CommandList.Add(commandCount, fh);
        //        commandCount++;
        //    }
        //}
        private OTcpServerMessage ReceiveBinary(ref TcpClient client)
        {
            // Get Header
            var receivedHeader = ReceiveHeader(ref client);
            var convertedType  = StringTypeToTypeConverter.GetTypeFrom(receivedHeader.ExpectType);

            // Once Header Received + Receive Data
            byte[] databuffer = new byte[receivedHeader.DataSize];


            try
            {
                client.GetStream().Read(databuffer, 0, databuffer.Length);
                object dataobject = Convert.ChangeType(ObjectBinarySerialization.ByteArrayToObject(databuffer), convertedType);
                return(new OTcpServerMessage(receivedHeader, dataobject));
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }