BinaryReader sendMsg(StreamOp op, Action <BinaryWriter> mkbody = null)
            {
                byte[] body;
                using (var ms = new MemoryStream())
                    using (var write = new BinaryWriter(ms))
                    {
                        write.Write(id);
                        write.Write((byte)op);
                        mkbody?.Invoke(write);
                        body = ms.ToArray();
                    }
                ServerResponseType resp = ServerResponseType.REQ_SUCCESS;
                BinaryReader       ret  = null;

                Task.WaitAll(man.SendMessage(ServerMessageType.STREAM_OP, body, (aresp, aret) => { resp = aresp; ret = aret == null ? null : new BinaryReader(new MemoryStream(aret.ReadBytes((int)aret.BaseStream.Length))); }));
                switch (resp) // TODO: Improve error handling
                {
                case ServerResponseType.REQ_SUCCESS:
                    break;

                case ServerResponseType.REQ_ERROR:
                    throw new Exception(ret.ReadString());

                default:
                    throw new Exception();
                }
                return(ret);
            }
Exemple #2
0
 public static ServerResponseCommand OK(ServerResponseType responseType, string message = "It's all OK!")
 {
     return(new ServerResponseCommand
     {
         statusCode = StatusCode.OK,
         responseType = responseType,
         Message = message
     });
 }
Exemple #3
0
 public static ServerResponseCommand Error(ServerResponseType responseType, string message = "An error occured")
 {
     return(new ServerResponseCommand
     {
         statusCode = StatusCode.Error,
         responseType = responseType,
         Message = message
     });
 }
Exemple #4
0
        public override void ParseFromString(string textToParse)
        {
            try
            {
                base.ParseFromString(textToParse);

                // get text to parse
                string responseTypeText = textToParse.Substring((int)Protocol.Instance.HeaderLength, (int)Protocol.Instance.ResponseTypeLength);

                _responseType = _serverResponseMapper[responseTypeText];
            }
            catch (System.Exception e)
            {
                throw e;
            }
        }
Exemple #5
0
 public ServerResponse(ServerResponseType Type, object Data)
 {
     this.Type = Type;
     this.Data = Data;
 }
Exemple #6
0
 public ServerResponse(ServerResponseType type, object data)
 {
     Type = type;
     Data = data;
 }
Exemple #7
0
 /// <summary>
 /// Used to return a response from server to client
 /// </summary>
 /// <param name="message">The message</param>
 /// <param name="responseType">The type of the response</param>
 public ServerResponse(string message, ServerResponseType responseType)
 {
     this.ResponseType = responseType;
     this.Message      = message;
 }